Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- typedef long long ll;
- #define pb push_back
- #define ff first
- #define ss second
- const int N=2e3+7;
- const int mod=1e9+7;
- const double eps=1e-9;
- const double pi= 3.14159265358979323846;
- using namespace std;
- using namespace __gnu_pbds;
- typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
- tree_order_statistics_node_update>
- op_set;
- int ans=0;
- map<int,int> dfs(int node,int parent,vector<vector<pair<int,int>>>&g,bool conn,int curr)
- {
- if(curr==0 && conn==1)
- ans++;
- map<int,int> mp;
- mp[curr]++;
- for(auto &[child,wt] : g[node])
- {
- if(child!=parent)
- {
- if(wt==0)
- {
- auto cmp=dfs(child,node,g,0,(curr^wt));
- int best=0;
- for(auto &[val,freq] : cmp)
- best=std::max(best,freq);
- ans+=best;
- }
- else
- {
- auto cmp=dfs(child,node,g,conn,(curr^wt));
- if(mp.size()<cmp.size())
- swap(mp,cmp);
- for(auto &[val,freq] : cmp)
- mp[val]+=freq;
- }
- }
- }
- return mp;
- }
- int32_t main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- int t;
- cin >> t;
- while(t--)
- {
- int n;
- cin >> n;
- vector<vector<pair<int,int>>> g(n+1);
- for(int i=1;i<n;i++)
- {
- int a,b,wt;
- cin >> a >> b >> wt;
- g[a].pb({b,wt});
- g[b].pb({a,wt});
- }
- ans=0;
- dfs(1,0,g,1,0);
- cout << ans-1 << "\n";
- }
- }
- /*
- God is King, we the soldiers , Ultra beam out the solar
- When I get to Heaven's gates . I ain't gotta peak over
- I ain't mean, I'm just focused
- Before the flood, people judge , They did the same thing to Noah
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement