-
Notifications
You must be signed in to change notification settings - Fork 0
/
Xor query
47 lines (35 loc) · 936 Bytes
/
Xor query
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <unordered_map>
int helper(int sv , vector <int> adj[] , unordered_map <int,int>&m)
{
int res = sv ;
for(auto itr : adj[sv])
{
int a = helper(itr , adj , m) ;
res = res ^ a ;
}
m[sv] = res ;
return res ;
}
vector<int> XORquery(int n, vector<vector<int>> &edges, int q, vector<int> &query) {
// Write your code here.
//construction of graph
vector <int> adj[n] ;
for(int i = 0 ; i < edges.size() ; i++)
{
adj[edges[i][0]].push_back(edges[i][1]);
}
//mapping it with xor
unordered_map <int,int> m;
helper(0 , adj , m) ;
// for(auto itr : m)
// {
// cout << itr.first << " " << itr.second << endl ;
// }
//getting ans
vector <int> ans ;
for(int i = 0 ; i < query.size() ; i++)
{
ans.push_back(m[query[i]]) ;
}
return ans ;
}