Advertisement
jiteshrao

Untitled

Nov 28th, 2020 (edited)
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 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. vector<pair<int, int>> query[N];
  8. int colour[N];
  9. int parent[N];
  10.  
  11. void dfs1(int sv, int par)
  12. {
  13.     parent[sv] = par;
  14.     for(int x : v[sv])
  15.     {
  16.         if(x == par)
  17.         {
  18.             continue;
  19.         }
  20.         dfs1(x, sv);
  21.     }
  22. }
  23.  
  24. void dfs2(int sv, int & cnt, int col)
  25. {
  26.     cnt += (colour[sv] == col);
  27.     for(int x : v[sv])
  28.     {
  29.         if(x == parent[sv])
  30.         {
  31.             continue;
  32.         }
  33.         dfs2(x, cnt, col);
  34.     }
  35. }
  36.  
  37. int32_t main()
  38. {
  39.     ios_base:: sync_with_stdio(false);
  40.     cin.tie(NULL);
  41.     cout.tie(NULL);
  42.     #ifndef ONLINE_JUDGE
  43.       freopen("input.txt", "r", stdin);
  44.       freopen("output.txt", "w", stdout);
  45.     #endif
  46.     int n, root, query;
  47.     cin >> n >> query >> root;
  48.     for(int i = 0; i < n - 1; i++)
  49.     {
  50.         int a, b;
  51.         cin >> a >> b;
  52.         v[a].push_back(b);
  53.         v[b].push_back(a);
  54.     }
  55.     for(int i = 1; i <= n; i++)
  56.     {
  57.         cin >> colour[i];
  58.     }
  59.     dfs1(root, 0);
  60.     for(int i = 1; i <= query; i++)
  61.     {
  62.         int node, colour;
  63.         cin >> node >> colour;
  64.         int cnt = 0;
  65.         dfs2(node, cnt, colour);
  66.         cout << cnt << endl;
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement