Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define fi first
- #define se second
- #define pb push_back
- #define FOR(i,a,b) for(int i=(a);i<=(b);++i)
- #define FORS(i,a,b) for(int i=(a);i<(b);++i)
- #define PII pair<int,int>
- #define vpp vector<PII>
- #define all(x) x.begin(),b.end()
- #define SZ(x) ((int)(x).size())
- #define ll long long
- #define maxn 150005
- int n;
- vector<int> sol[maxn];
- int where[maxn];
- int find(int poz){
- if(poz==where[poz]) return poz;
- return where[poz]=find(where[poz]);//reorientate
- }
- void unite(int a,int b){
- a=find(a);
- b=find(b);
- if(SZ(sol[a])<SZ(sol[b])) swap(a,b);
- for(int x:sol[b])
- sol[a].pb(x);
- where[b]=a;
- }
- int main()
- {
- //freopen("pluton.in","r",stdin);
- //freopen("pluton.out","w",stdout);
- int x,y;
- cin>>n;
- FOR(i,1,n)
- where[i]=i,sol[i].pb(i);
- FORS(i,1,n){
- cin>>x>>y;
- unite(x,y);
- }
- for(int a:sol[find(1)]) cout<<a<<' ';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement