jiteshrao

Untitled

Nov 28th, 2020 (edited)
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1e5 + 5;
  5.  
  6. vector<int> v[N];
  7. int colour[N];
  8. int ret[N];
  9. map<int, int> sets[N];
  10. vector<pair<int, int>> store[N];
  11.  
  12. void dfs(int sv, int par)
  13. {
  14.     for(int x : v[sv])
  15.     {
  16.         if(x == par)
  17.         {
  18.             continue;
  19.         }
  20.         dfs(x, sv);
  21.         for(pair<int, int> el : sets[x])
  22.         {
  23.             sets[sv][el.first] += el.second;
  24.         }
  25.     }
  26.     for(pair<int, int> x : store[sv])
  27.     {
  28.         ret[x.second] = sets[sv][x.first];
  29.     }
  30. }
  31.  
  32. int32_t main()
  33. {
  34.     ios_base:: sync_with_stdio(false);
  35.     cin.tie(NULL);
  36.     cout.tie(NULL);
  37.     #ifndef ONLINE_JUDGE
  38.       freopen("input.txt", "r", stdin);
  39.       freopen("output.txt", "w", stdout);
  40.     #endif
  41.     int n, root, query;
  42.     cin >> n >> query >> root;
  43.     for(int i = 0; i < n - 1; i++)
  44.     {
  45.         int a, b;
  46.         cin >> a >> b;
  47.         v[a].push_back(b);
  48.         v[b].push_back(a);
  49.     }
  50.     for(int i = 1; i <= n; i++)
  51.     {
  52.         cin >> colour[i];
  53.         sets[i][colour[i]]++;
  54.     }
  55.     for(int i = 1; i <= query; i++)
  56.     {
  57.         int node, colour;
  58.         cin >> node >> colour;
  59.         store[node].push_back({colour, i});
  60.     }
  61.     dfs(root, 0);
  62.     for(int i = 1; i <= n; i++)
  63.     {
  64.         cout << ret[i] << " ";
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment