Accurate XOR Codechef Problem Solution ( TREEXOR )
Chef has a tree consisting of
Chef wants to assign a binary value (0
or 1
) to every node of the tree. The Xor-value of a node is defined as the bitwise XOR of all the binary values present in the subtree of that node.
Help Chef to assign values to the nodes of the tree in such a way that the sum of Xor-value of all the
It can be shown that such an assignment always exists. If there are multiple possible answers, you may print any of them.
Input Format
- The first line of input will contain a single integer
T , denoting the number of test cases. - The first line of each test case contains two space-separated integers
N andK , denoting the number of vertices in the tree and the required sum of Xor-value respectively. - The second line of each test case contains
N−1 space-separated integersP2,P3,…,PN , wherePi denotes the parent-node of theith node.
Output Format
For each test case, print on a new line a binary string of length
Constraints
1 ≤ T ≤ 10^5 2 ≤ N ≤ 2⋅10^5 0 ≤ K ≤ N 1 ≤ Pi < i for each2 ≤ i ≤ N - The sum of
N over all test cases won’t exceed2⋅10^5 .
Sample Input 1
3
2 0
1
3 1
1 1
5 2
1 2 2 1
Sample Output 1
00
100
11010
Explanation
Test case
Test case
Test case
using namespace std;
int done;
int dfs(vector>&adj,int x,vector&ans,vector&vi){
int y=0;
for(auto j:adj[x]){
if(vi[j])continue;
vi[j]=true;
y+=dfs(adj,j,ans,vi);
}
if(done){
ans[x]='0'+1-(y%2);
done--;
return 1;
}
ans[x]='0'+(y%2);
return 0;
}
int main() {
int T;
cin >> T;
while(T--){
int n,k;
cin >> n >> k;
vector>adj(n+1);
for(int i=0;i> t;
adj[i+2].push_back(t);
adj[t].push_back(i+2);
}
done=k;
vectorans(n+1,'0');
vectorvi(n+1,false);
vi[1]=true;
int m=dfs(adj,1,ans,vi);
for(int i=1;i<=n;i++)cout << ans[i];
cout << endl;
}
return 0;
}
0 Comments