Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define endl "\n"
- using namespace std;
- using ll = long long;
- using ld = long double;
- using pii = pair<int, int>;
- constexpr int N = 1e6+1;
- int n, ro;
- vector<int> gr[N];
- set<int> vec[N];
- int ans[N], id[N];
- bool used[N];
- void dfs(int i = ro)
- {
- used[i] = true;
- for (int j: gr[i])
- if (!used[j])
- {
- dfs(j);
- if (vec[id[i]].size() < vec[id[j]].size())
- swap(id[i], id[j]);
- for (int el: vec[id[j]])
- vec[id[i]].insert(el);
- vec[id[j]].clear();
- }
- gr[i].clear();
- ans[i] = vec[id[i]].size();
- }
- void Solve()
- {
- cin >> n;
- for (int i = 1, a, b; i <= n; i++)
- {
- cin >> a >> b;
- gr[a].push_back(i);
- vec[i].insert(b);
- if (a == 0)
- ro = i;
- }
- iota(id, id+N, 0);
- dfs();
- for (int i = 1; i <= n; i++)
- cout << ans[i] << ' ';
- }
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- Solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement