Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int N = 1e5 + 5;
- vector<int> v[N];
- int colour[N];
- int parent[N];
- void dfs1(int sv, int par)
- {
- parent[sv] = par;
- for(int x : v[sv])
- {
- if(x == par)
- {
- continue;
- }
- dfs1(x, sv);
- }
- }
- void dfs2(int sv, set<int> & s)
- {
- s.insert(colour[sv]);
- for(int x : v[sv])
- {
- if(x == parent[sv])
- {
- continue;
- }
- dfs2(x, s);
- }
- }
- int32_t main()
- {
- ios_base:: sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- int n, root, query;
- cin >> n >> query >> root;
- for(int i = 0; i < n - 1; i++)
- {
- int a, b;
- cin >> a >> b;
- v[a].push_back(b);
- v[b].push_back(a);
- }
- for(int i = 1; i <= n; i++)
- {
- cin >> colour[i];
- }
- dfs1(root, 0);
- for(int i = 1; i <= query; i++)
- {
- int node;
- cin >> node;
- set<int> s;
- dfs2(node, s);
- cout << (int)s.size() << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement