Skip to content

Latest commit

 

History

History
108 lines (103 loc) · 3.2 KB

File metadata and controls

108 lines (103 loc) · 3.2 KB

Algorithm: Tree

Problem: Lomsat gelral

#include <bits/stdc++.h>
using namespace std;
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define vpii vector<pair<int,int>>
#define vpll vector<pair<ll,ll>>
int Set(int N, int pos) {return  N = N | (1<<pos);}
int Reset(int N, int pos) {return  N = N & ~(1<<pos);}
bool Cheek(int N, int pos) {return  (bool)(N & (1<<pos));}
#define least_one_pos(x) __builtin_ffs(x)
#define leading_zeros(x) __builtin_clz(x)
#define tailing_zeros(x) __builtin_ctz(x)
#define num_of_one(x) __builtin_popcount(x)
///............x...........///
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define ft cout << "for test"<<endl;
#define print(v) for (auto it : v)cout << it<<" ";cout << endl;
#define PI acos(-1.0)
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define t_c int test, cs = 1;cin>>test;while (test--)
///................function.....................///

//#define mod 1000000007
//........constant........///
const ll N = 1e5+5;
void file(){
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
}
class Tree{
public:
    vector<vector<int>> adj;
    vi clr,shadow;
    vll Max,Tot,res;
    vector<map<ll,ll>>color;
    int n;
    Tree(int _n){
        n=_n;
        adj.resize(n+1);
        clr = shadow = vi(n+1);
        Max = Tot = res = vll(n+1);
        color.resize(n+1);
    }
    void readTree(){
        for(int i=1; i<=n; i++)cin>>clr[i];
        int a,b;
        for(int i=1; i<n; i++){
            cin>>a>>b;
            adj[a].pb(b);
            adj[b].pb(a);
        }
        dfs(1,-1);
        for(int i=1; i<=n; i++)cout<<res[i]<<" \n"[i==n];
    }
    void dfs(int nd, int par){
        shadow[nd] = nd;
        Max[nd] = 1;
        color[nd][clr[nd]]++;
        Tot[nd] = clr[nd];
        for(auto to:adj[nd]){
            if(to==par)continue;
            dfs(to,nd);
            if(color[shadow[nd]].size()<color[shadow[to]].size())
                swap(shadow[nd],shadow[to]);

            for(auto x:color[shadow[to]]){
                color[shadow[nd]][x.first] += x.second;
                // cout<<nd<<" "<<to<<" "<<Max[shadow[nd]]<<" "<< color[shadow[nd]][x.first]<<" "<<x.first<<endl;
                if(Max[shadow[nd]] < color[shadow[nd]][x.first]){
                    Max[shadow[nd]] = color[shadow[nd]][x.first];
                    Tot[shadow[nd]] = x.first;
                }
                else if(Max[shadow[nd]] == color[shadow[nd]][x.first]){
                    Tot[shadow[nd]] += x.first;
                }
            }

        }
        res[nd] = Tot[shadow[nd]];
    }
};
int main(){
   FIO;
   // file();
   int n,i,j;
   cin>>n;
   Tree obj(n);
   obj.readTree();

}