-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfreaky_tree.cpp
More file actions
57 lines (52 loc) · 865 Bytes
/
freaky_tree.cpp
File metadata and controls
57 lines (52 loc) · 865 Bytes
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
48
49
50
51
52
53
54
55
56
57
// https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/practice-problems/algorithm/freaky-tree/description/
#include<bits/stdc++.h>
using namespace std;
vector<vector<pair<long int,long int>>>v(100000);
int cont=0,ans=0;
int vis[100000]={0};
int dfs(long int i)
{
long int j;
int flag=0;
vis[i]=1;
for(j=0;j<v[i].size();j++)
{
if(vis[v[i][j].first]==0)
{
if((v[i][j].second)%2==1)
cont++;
dfs(v[i][j].first);
flag=1;
}
}
if(flag==0)
{
if(cont%2==1)
ans++;
}
vis[i]=0;
if((v[i][0].second)%2==1)
cont--;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cont=0,ans=0;
memset(vis,0,sizeof(vis));
long int n,i,x,y,z;
for(i=0;i<100000;i++)
v[i].clear();
cin>>n;
for(i=0;i<n-1;i++)
{
cin>>x>>y>>z;
v[x].push_back({y,z});
v[y].push_back({x,z});
}
dfs(1);
cout<<ans<<endl;
}
}