MAGCARI

Sack

Feb 25th, 2023
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. /*
  2.     Task    : _example
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 26 February 2023 [09:06]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. vector<int > g[1010];
  10. map<int ,int > cnt[1010];
  11. int col[1010];
  12. // cnt[i][j] is the number of vertices in subtree of vertex i that has color j
  13. void dfs(int u,int p){
  14.     int mx = -1,bigChild = -1;
  15.     for(auto x:g[u]){
  16.         if(x == p)  continue;
  17.         dfs(x,u);
  18.         if(mx < cnt[x].size()){
  19.             mx = cnt[x].size();
  20.             bigChild = x;
  21.         }
  22.     }
  23.    
  24.     if(bigChild != -1)
  25.         cnt[u] = cnt[bigChild];
  26.     cnt[u][col[u]]++;
  27.  
  28.     for(auto x:g[u]){
  29.         if(x == p || x == bigChild) continue;
  30.         for(auto y:cnt[x])
  31.             cnt[u][y.first]+=y.second;
  32.     }
  33. }
  34. int main(){
  35.     cin.tie(0)->sync_with_stdio(0);
  36.     cin.exceptions(cin.failbit);
  37.  
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment