C. Mail Stamps
Time Limit:20 Sec
Memory limit:256 MB
Topic Connection
Http://codeforces.com/problemset/problem/29/C
Descriptionone Day Bob got a 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, T Hat ' s Why the letter is sent via some intermediate cities. Post officers never send a letter in such a is the the route of this letter contains some city more than once. Bob is sure that the post officers stamp the letters accurately.
There is n stamps on the envelope of Bob's letter. He understands the possible routes of this is only. But the stamps is numerous, and Bob can ' t determine himself none of these routes. That's why he asks your to help him. Find one of the possible routes of the letter.
Input
The first line contains an integer n (1≤n≤105)-amount of mail stamps on the envelope. Then there follow n lines with the integers each-description of the stamps. Each stamp are described with indexes of the cities between which a, is sent. The indexes of cities is integers from 1 to 109. Indexes of all the cities is different. Every time the letter was sent from a city to another, exactly one stamp was put on the envelope. It is guaranteed, the given stamps correspond to some valid, route from some city to some.
Output
Output n + 1 numbers-indexes of cities in one of the both possible routes of the letter.
Sample Input
2
1 100
100 2
Sample Output
2 100 1
HINT
Test instructions
Give you a chain that lets you output from the beginning to the end
Exercises
Discretization, and then in the run-up topology sort of just fine
Code
//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 2000001#defineMoD 1000000007#defineEPS 1e-9intNum;Charch[ -];Const intinf=0x3f3f3f3f; inline ll read () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//**************************************************************************************intN;vector<int>Q;map<int,int>H;map<int,int>h;intA[MAXN];intB[maxn];vector<int>E[MAXN];intD[MAXN];intVIS[MAXN];intMain () {n=read (); for(intI=0; i<n;i++) {A[i]=read (); B[i]=read (); Q.push_back (A[i]); Q.push_back (B[i]); } sort (Q.begin (), Q.end ()); Q.erase (Unique (Q.begin (), Q.end ()), Q.end ()); for(intI=0; I<q.size (); i++) H[q[i]]=i,h[i]=Q[i]; for(intI=0; i<n;i++) {e[h[a[i]]].push_back (h[b[i]); E[h[b[i]]].push_back (H[a[i]); D[h[a[i] ]++; D[h[b[i] ]++; } intflag=1; Queue<int>QQ; for(intI=0; I<q.size (); i++) { if(d[h[q[i]]]==1) {Qq.push (h[q[i]); Vis[h[q[i] ]=1; Break; } } while(!Qq.empty ()) { intv=Qq.front (); printf ("%d", H[v]); VIS[V]=1; Qq.pop (); for(intI=0; I<e[v].size (); i++) { if(Vis[e[v][i]])Continue; D[e[v][i]]--; if(d[e[v][i]]<=1) Qq.push (E[v][i]); } }}
Codeforces Beta Round #29 (Div. 2, Codeforces format) c. Mail stamps discretization topology sort