Advertisement
limimage

task J

Sep 5th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define endl "\n"
  4. using namespace std;
  5. using ll = long long;
  6. using ld = long double;
  7. using pii = pair<int, int>;
  8.  
  9. constexpr int N = 1e6+1;
  10.  
  11. int n, ro;
  12. vector<int> gr[N];
  13. set<int> vec[N];
  14. int ans[N], id[N];
  15. bool used[N];
  16.  
  17. void dfs(int i = ro)
  18. {
  19. used[i] = true;
  20. for (int j: gr[i])
  21. if (!used[j])
  22. {
  23. dfs(j);
  24. if (vec[id[i]].size() < vec[id[j]].size())
  25. swap(id[i], id[j]);
  26. for (int el: vec[id[j]])
  27. vec[id[i]].insert(el);
  28. vec[id[j]].clear();
  29. }
  30. gr[i].clear();
  31. ans[i] = vec[id[i]].size();
  32. }
  33.  
  34. void Solve()
  35. {
  36. cin >> n;
  37. for (int i = 1, a, b; i <= n; i++)
  38. {
  39. cin >> a >> b;
  40. gr[a].push_back(i);
  41. vec[i].insert(b);
  42. if (a == 0)
  43. ro = i;
  44. }
  45. iota(id, id+N, 0);
  46. dfs();
  47. for (int i = 1; i <= n; i++)
  48. cout << ans[i] << ' ';
  49. }
  50.  
  51. int main()
  52. {
  53. ios::sync_with_stdio(false);
  54. cin.tie(nullptr);
  55. cout.tie(nullptr);
  56. Solve();
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement