• cf600E. Lomsat gelral


    题解:  dsu on tree 实质上启发式合并 我们考虑维护节点u的信息 需要将u的儿子的信息和节点信息合并 我们考虑启发式合并 把轻儿子合并到重儿子上 我们考虑n次启发式合并 复杂度nlogn*(合并一次的复杂度)  具体做法那我们考虑树链剖分的时候 划分轻重儿子 保留重儿子信息 清空轻儿子信息 并将轻儿子的信息合并到重儿子上即可

    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <cmath>
    #include <set>
    #include <map>
    #define mp make_pair
    #define pb push_back
    #define pii pair<int,int>
    #define link(x) for(edge *j=h[x];j;j=j->next)
    #define inc(i,l,r) for(int i=l;i<=r;i++)
    #define dec(i,r,l) for(int i=r;i>=l;i--)
    const int MAXN=3e5+10;
    const double eps=1e-8;
    #define ll long long
    using namespace std;
    struct edge{int t;edge*next;}e[MAXN<<1],*h[MAXN],*o=e;
    void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;}
    ll read(){
        ll x=0,f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
        while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
        return x*f;
    }
    int a[MAXN];
    ll ans[MAXN];int cnt[MAXN],cc[MAXN];
    int num[MAXN],son[MAXN];
    void dfs(int x,int fa){
    	num[x]=1;
    	link(x){
    		if(j->t!=fa){
    			dfs(j->t,x);
    			num[x]+=num[j->t];
    			if(son[x]==-1||num[son[x]]<num[j->t])son[x]=j->t;
    		}
    	}
    }
    void Add(int pos,int key){
    	cnt[key]++;
    	if(cnt[key]==cc[pos])ans[pos]+=key;
    	else if(cnt[key]>cc[pos])ans[pos]=key,cc[pos]=cnt[key];
    }
    void add(int x,int fa,int pos){
    	Add(pos,a[x]);
    	link(x){
    		if(j->t!=fa)add(j->t,x,pos);
    	}
    }
    void clear(int x,int fa){
    	cnt[a[x]]--;
    	link(x){
    		if(j->t!=fa)clear(j->t,x);
    	}
    }
    void dfs1(int x,int fa){
    	link(x){
    		if(j->t==fa||j->t==son[x])continue;
    		dfs1(j->t,x);
    		clear(j->t,x);
    	}
    	if(son[x]!=-1)dfs1(son[x],x),cc[x]=cc[son[x]],ans[x]=ans[son[x]];
    	link(x){
    		if(j->t==fa||j->t==son[x])continue;
    		add(j->t,x,x);
    	}
    	Add(x,a[x]);
    }
    int main(){
    	int n=read();
    	inc(i,1,n)a[i]=read(),son[i]=-1;
    	int u,v;
    	inc(i,1,n-1)u=read(),v=read(),add(u,v),add(v,u);
    	dfs(1,0);dfs1(1,0);
    	inc(i,1,n)printf("%lld ",ans[i]);
    	printf("
    ");
    }
    
    E. Lomsat gelral
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour.

    Let's call colour c dominating in the subtree of vertex v if there are no other colours that appear in the subtree of vertex v more times than colour c. So it's possible that two or more colours will be dominating in the subtree of some vertex.

    The subtree of vertex v is the vertex v and all other vertices that contains vertex v in each path to the root.

    For each vertex v find the sum of all dominating colours in the subtree of vertex v.

    Input

    The first line contains integer n (1 ≤ n ≤ 105) — the number of vertices in the tree.

    The second line contains n integers ci (1 ≤ ci ≤ n), ci — the colour of the i-th vertex.

    Each of the next n - 1 lines contains two integers xj, yj (1 ≤ xj, yj ≤ n) — the edge of the tree. The first vertex is the root of the tree.

    Output

    Print n integers — the sums of dominating colours for each vertex.

    Examples
    input
    Copy
    4
    1 2 3 4
    1 2
    2 3
    2 4
    output
    Copy
    10 9 3 4
    input
    Copy
    15
    1 2 3 1 2 3 3 1 1 3 2 2 1 2 3
    1 2
    1 3
    1 4
    1 14
    1 15
    2 5
    2 6
    2 7
    3 8
    3 9
    3 10
    4 11
    4 12
    4 13
    output
    Copy
    6 5 4 3 2 3 3 1 1 3 2 2 1 2 3
  • 相关阅读:
    web服务器-Apache
    nginx优化
    nginx下载限速
    nginx-URL重写
    HDU 5358 First One 求和(序列求和,优化)
    HDU 5360 Hiking 登山 (优先队列,排序)
    HDU 5353 Average 糖果分配(模拟,图)
    UVALive 4128 Steam Roller 蒸汽式压路机(最短路,变形) WA中。。。。。
    HDU 5348 MZL's endless loop 给边定向(欧拉回路,最大流)
    HDU 5344 MZL's xor (水题)
  • 原文地址:https://www.cnblogs.com/wang9897/p/9906451.html
Copyright © 2020-2023  润新知