Advertisement
Guest User

overkill | not recommended

a guest
Mar 16th, 2023
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. typedef long long ll;
  5. #define pb push_back
  6. #define ff first
  7. #define ss second
  8. const int N=2e3+7;
  9. const int mod=1e9+7;
  10. const double eps=1e-9;
  11. const double pi=    3.14159265358979323846;
  12. using namespace std;  
  13. using namespace __gnu_pbds;
  14. typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
  15.              tree_order_statistics_node_update>
  16.     op_set;
  17.  
  18. int ans=0;
  19.  
  20. map<int,int> dfs(int node,int parent,vector<vector<pair<int,int>>>&g,bool conn,int curr)
  21. {
  22.     if(curr==0 && conn==1)
  23.         ans++;
  24.  
  25.     map<int,int> mp;
  26.     mp[curr]++;
  27.     for(auto &[child,wt] : g[node])
  28.     {
  29.         if(child!=parent)
  30.         {
  31.             if(wt==0)
  32.             {
  33.                 auto cmp=dfs(child,node,g,0,(curr^wt));
  34.                 int best=0;
  35.                 for(auto &[val,freq] : cmp)
  36.                     best=std::max(best,freq);
  37.                 ans+=best;
  38.             }
  39.             else
  40.             {
  41.                 auto cmp=dfs(child,node,g,conn,(curr^wt));
  42.                 if(mp.size()<cmp.size())
  43.                     swap(mp,cmp);
  44.                 for(auto &[val,freq] : cmp)
  45.                     mp[val]+=freq;
  46.             }
  47.         }
  48.     }
  49.  
  50.     return mp;
  51. }
  52.  
  53. int32_t main()
  54. {
  55.    ios_base::sync_with_stdio(false);
  56.    cin.tie(0);
  57.  
  58.    int t;
  59.    cin >> t;
  60.    while(t--)
  61.    {
  62.         int n;
  63.         cin >> n;
  64.         vector<vector<pair<int,int>>> g(n+1);
  65.         for(int i=1;i<n;i++)
  66.         {
  67.             int a,b,wt;
  68.             cin >> a >> b >> wt;
  69.             g[a].pb({b,wt});
  70.             g[b].pb({a,wt});
  71.         }
  72.  
  73.         ans=0;
  74.         dfs(1,0,g,1,0);
  75.  
  76.         cout << ans-1 << "\n";
  77.    }
  78. }
  79.  
  80. /*
  81. God is King, we the soldiers , Ultra beam out the solar
  82. When I get to Heaven's gates . I ain't gotta peak over
  83. I ain't mean, I'm just focused
  84. Before the flood, people judge , They did the same thing to Noah
  85. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement