zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 拓扑排序

    C. Mail Stamps
     
     

    One day Bob got a letter in an envelope. Bob knows that when Berland's post officers send a letter directly from city «A» to city «B», they stamp it with «A B», or «B A». Unfortunately, often it is impossible to send a letter directly from the city of the sender to the city of the receiver, that's why the letter is sent via some intermediate cities. Post officers never send a letter in such a way that the route of this letter contains some city more than once. Bob is sure that the post officers stamp the letters accurately.

    There are n stamps on the envelope of Bob's letter. He understands that the possible routes of this letter are only two. But the stamps are numerous, and Bob can't determine himself none of these routes. That's why he asks you to help him. Find one of the possible routes of the letter.

    Input

    The first line contains integer n (1 ≤ n ≤ 105) — amount of mail stamps on the envelope. Then there follow n lines with two integers each — description of the stamps. Each stamp is described with indexes of the cities between which a letter is sent. The indexes of cities are integers from 1 to 109. Indexes of all the cities are different. Every time the letter is sent from one city to another, exactly one stamp is put on the envelope. It is guaranteed that the given stamps correspond to some valid route from some city to some other city.

    Output

    Output n + 1 numbers — indexes of cities in one of the two possible routes of the letter.

    Examples
    input
    2
    1 100
    100 2
    output
    2 100 1 
    input
    3
    3 1
    100 2
    3 2
    output
    100 2 3 1 

    题意:

      给你n张邮票

      每张邮票上面写有两个城市a,b

      有可能是a到b

      也有可能是b到a

         现在问你 一条可行的路线

    题解:

      很明显的拓扑

      每次选择度小于等于1 的点加入答案就好了

      注意数据范围

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include<vector>
    #include<map>
    #include<queue>
    using namespace std;
    const int N = 1e5+10, M = 30005, mod = 1000000007, inf = 0x3f3f3f3f;
    typedef long long ll;
    
    int n,a[N],b[N],vis[N],cnt = 1;
    vector<int > G[N];
    map<int,int > mp,hash,fhash;
    int main() {
        scanf("%d",&n);
        for(int i=1;i<=n;i++) {
            scanf("%d%d",&a[i],&b[i]);
            if(!hash.count(a[i])) hash[a[i]] = cnt ,fhash[cnt++] = a[i];
             if(!hash.count(b[i])) hash[b[i]] = cnt, fhash[cnt++] = b[i];
             a[i] = hash[a[i]];
             b[i] = hash[b[i]];
        }
        for(int i=1;i<=n;i++) {
            int aa = a[i], bb = b[i];
            G[aa].push_back(bb);
            G[bb].push_back(aa);
            mp.count(aa)?mp[aa]++:mp[aa]=1;
            mp.count(bb)?mp[bb]++:mp[bb]=1;
        }
        int u = 0;
        for(map<int,int >::iterator it=mp.begin();it!=mp.end();it++) {
            int now = it->first;
            if(mp[now]==1) u = now;
        }
        queue<int> q;
        vector<int >ans;
        q.push(u);
       // cout<<fhash[u]<<endl;
        memset(vis,0,sizeof(vis));
        while(!q.empty()) {
            int k = q.front();
            q.pop();
            vis[k] = 1;
            ans.push_back(k);
            for(int i=0;i<G[k].size();i++) {
                //if(vis[G[k][i]]) continue;
                mp[G[k][i]] --;
                if(mp[G[k][i]]<=1&&!vis[G[k][i]]) q.push(G[k][i]);
            }
        }
        for(int i=0;i<ans.size();i++) {
                //cout<<ans[i]<<endl;
            printf("%d ",fhash[ans[i]]) ;
        }
        return 0;
    }
  • 相关阅读:
    [导入]mootools框架【二】Core篇: 主要方法测试实例
    公司招聘中不能说的秘密 【转载】
    国外10个ASP.Net C#下的开源CMS
    [导入][Flash开发笔记] List控件删除指定label或data的项
    [导入]用C#截取指定长度的中英文混合字符串 改进版
    今天小侄子出生,想了一天的名字
    一个正则表达式的解释
    今日小收获
    昨天的事
    两点东西
  • 原文地址:https://www.cnblogs.com/zxhl/p/5414154.html
Copyright © 2011-2022 走看看