Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*#pragma GCC optimize("Ofast,no-stack-protector")
- #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- #pragma GCC optimize("unroll-loops")
- #pragma GCC optimize("fast-math")*/
- #include <stdio.h>
- #include <bits/stdc++.h>
- using namespace std;
- #define vec vector
- #define pb push_back
- #define all(x) x.begin(), x.end()
- #define rall(x) x.rbegin(), x.rend()
- #define Str(x) to_string(x)
- #define len(s) (int)s.size()
- #define int long long
- typedef long long ll;
- typedef double d;
- typedef long double lld;
- typedef string str;
- typedef unsigned long long ull;
- typedef unsigned int ui;
- const int MAXN = 160000;
- vector <int> fck[MAXN], rt(MAXN), cnt(MAXN, 1);
- int get(int x)
- {
- return x == rt[x] ? x : rt[x] = get(rt[x]);
- }
- void Union(int x, int y)
- {
- x = get(x);
- y = get(y);
- if (cnt[x] < cnt[y])
- swap(x, y);
- for (auto it : fck[y])
- fck[x].pb(it);
- fck[y].clear();
- cnt[x] += cnt[y];
- cnt[y] = 0;
- rt[y] = x;
- }
- main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- #ifdef LOCAL
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- int n;
- cin >> n;
- for (int i = 0; i < n; i++)
- {
- rt[i] = i;
- fck[i].pb(i);
- }
- for (int i = 0; i < n - 1; i++)
- {
- int x, y;
- cin >> x >> y;
- x--;
- y--;
- Union(x, y);
- }
- for (int i =0 ; i < n; i++)
- {
- if (cnt[i] == n)
- {
- for (auto it : fck[i])
- cout << it + 1<< " ";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement