Nuttela loves trees ! wrong answer

what is wrong i my approach?

#include <bits/stdc++.h>

using namespace std;
// Driver program to test a
int main()
{
int t;cin>>t;
while(t–)
{
int n;
cin>>n;
int count[n+1]={0};
int a[n+1]={0};
for(int i=1;i<=n;i++)
{
int d;
cin>>d;
a[i]=d;
}
int q;
vector ad[n+1];
for(int i=1;i<=n-1;i++)
{
int d,f;
cin>>d>>f;
count[d]+=a[f];
count[f]+=a[d];

	ad[d].push_back(f);
	ad[f].push_back(d);
}

cin>>q;
int o,p;

for(int i=0;i<q;i++)
{
cin>>o>>p;

if(o==1)
{
// updation
if(a[p]==0)
{
a[p]=1;
for(auto s=0;s<ad[p].size();s++)
{
count[ad[p][s]]+=1;
}

	}
	else
	{
	a[p]=0;
	for(auto s=0;s<ad[p].size();s++)
	{
	count[ad[p][s]]-=1;
	}
		
	}

}
else
cout<<count[p]<<endl;
}

}
}

Here is my AC code for reference:- https://www.codechef.com/viewsolution/26095747

2 Likes

This problem is Subtree Query type problem. Here you need to use DFS for Euler Tour and Segment Tree for query and update. Editorial of Encoding Aug’19 will be publish soon then you will better understand.

2 Likes

so much clean code yaar