zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 2 E. Lomsat gelral 启发式合并map

    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
    4
    1 2 3 4
    1 2
    2 3
    2 4
    output
    10 9 3 4
    input
    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
    6 5 4 3 2 3 3 1 1 3 2 2 1 2 3

    题目链接:http://codeforces.com/contest/600/problem/E
    题意:给你一颗树,找出每个子树下的众数之和;

    思路:启发式合并map;

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-14
    #define bug(x)  cout<<"bug"<<x<<endl;
    const int N=1e5+10,M=4e6+10,inf=2147483647;
    const ll INF=1e18+10,mod=1e9+7;
    struct is
    {
        int v,next;
    }edge[N<<1];
    int head[N],edg;
    void init()
    {
        edg=0;
        memset(head,-1,sizeof(head));
    }
    void add(int u,int v)
    {
        edg++;
        edge[edg].v=v;
        edge[edg].next=head[u];
        head[u]=edg;
    }
    ll ans[N],sum[N];
    int a[N];
    int maxx[N];
    map<int,int>mp[N];
    map<int,int>::iterator it;
    
    void update(int a,int b)
    {
        if(mp[a].size()<mp[b].size())
        {
            swap(maxx[a],maxx[b]);
            swap(sum[a],sum[b]);
            swap(mp[a],mp[b]);
        }
        while(mp[b].size())
        {
            it=mp[b].begin();
            mp[a][it->first]+=it->second;
            if(maxx[a]==mp[a][it->first])
                sum[a]+=it->first;
            else if(maxx[a]<mp[a][it->first])
            {
                maxx[a]=mp[a][it->first];
                sum[a]=it->first;
            }
            mp[b].erase(it);
        }
    }
    
    void dfs(int u,int fa)
    {
        //cout<<u<<" "<<fa<<endl;
        mp[u][a[u]]++;
        maxx[u]=1;
        sum[u]=a[u];
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(v==fa)continue;
            dfs(v,u);
            update(u,v);
        }
        ans[u]=sum[u];
    }
    ///   数组大小
    int main()
    {
        init();
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v),add(v,u);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++)
            printf("%lld ",ans[i]);
        return 0;
    }
    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
    4
    1 2 3 4
    1 2
    2 3
    2 4
    output
    10 9 3 4
    input
    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
    6 5 4 3 2 3 3 1 1 3 2 2 1 2 3
  • 相关阅读:
    常用网络操作命令
    C语言中的位域[转]
    状态机——一种强大的思想利器
    9030PCI CAN驱动开发点滴
    驱动开发中应该注意的事项
    java 从网络Url中下载文件
    windows pyspider 爬虫安装
    java list去重
    Java 文件分块及合并
    工程部署到linux
  • 原文地址:https://www.cnblogs.com/jhz033/p/6669856.html
Copyright © 2011-2022 走看看