• 点分治 (等级排) codeforces 321C


    Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has n cities connected by n - 1 undirected roads, and for any two cities there always exists a path between them.

    Fox Ciel needs to assign an officer to each city. Each officer has a rank — a letter from 'A' to 'Z'. So there will be 26 different ranks, and 'A' is the topmost, so 'Z' is the bottommost.

    There are enough officers of each rank. But there is a special rule must obey: if x and y are two distinct cities and their officers have the same rank, then on the simple path between x and y there must be a city z that has an officer with higher rank. The rule guarantee that a communications between same rank officers will be monitored by higher rank officer.

    Help Ciel to make a valid plan, and if it's impossible, output "Impossible!".

    Input

    The first line contains an integer n (2 ≤ n ≤ 105) — the number of cities in Tree Land.

    Each of the following n - 1 lines contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — they mean that there will be an undirected road between a and b. Consider all the cities are numbered from 1 to n.

    It guaranteed that the given graph will be a tree.

    Output

    If there is a valid plane, output n space-separated characters in a line — i-th character is the rank of officer in the city with number i.

    Otherwise output "Impossible!".

    Example
    Input
    4
    1 2
    1 3
    1 4
    Output
    A B B B
    Input
    10
    1 2
    2 3
    3 4
    4 5
    5 6
    6 7
    7 8
    8 9
    9 10
    Output
    D C B A D C B D C D
    Note

    In the first example, for any two officers of rank 'B', an officer with rank 'A' will be on the path between them. So it is a valid solution.

    题目分析 : 给你一棵树,A,B,C... 表示等级,现要求两个相同等级的之间必须有一个比它大的字母,输出所有的字母序

    思路分析 : 每次找树的重心,将它标记一个字母即可,因为找重心每次是减少一半的点,因此最多可以标记整个树是 2^25 个点的树

    代码示例 :

    const int maxn = 1e5+5;
    const int inf = 0x3f3f3f3f;
    #define ll long long
    
    int num = 0;
    vector<int>ve[maxn];
    int balance, root;
    bool done[maxn];
    int size[maxn], mx[maxn];
    char ans[maxn];
    
    void getroot(int x, int fa){
        size[x] = 1, mx[x] = 0; //以当前结点为根节点的最大结点个数
        
        for(int i = 0; i < ve[x].size(); i++){
            int to = ve[x][i];
            
            if (to == fa || done[to]) continue;
            getroot(to, x);
            size[x] += size[to];
            mx[x] = max(mx[x], size[to]);
        }
        mx[x] = max(mx[x], num-size[x]);
        if (mx[x] < balance) {balance = mx[x], root = x;}
    }
    
    void dfs(int x, int k){
        done[x] = true;
        ans[x] = 'A'+k;
        
        for(int i = 0; i < ve[x].size(); i++){
            int to = ve[x][i];
            
            if (done[to]) continue;
            balance = inf, num = size[to];
            getroot(to, to);
            dfs(root, k+1);        
        }
    }
    
    int main() {
        int n;
        int a, b;
        
        cin >> n;
        for(int i = 1; i < n; i++){
            scanf("%d%d", &a, &b);
            ve[a].push_back(b);
            ve[b].push_back(a);        
        }
        memset(done, false, sizeof(done));
        balance = inf, num = n;
        getroot(1, 1);
        //printf("root = %d
    ", root);
        dfs(root, 0);
        for(int i = 1; i <= n; i++) printf("%c%c",ans[i], i==n?'
    ':' ');
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    人生苦短之Python的urllib urllib2 requests
    近期测试BUG总结
    人生苦短之Python列表拷贝
    测试发展前景,测试人员的发展方向,测试趋势
    人生苦短之Python函数的健壮性
    Python视频教程
    人生苦短之Python文件的IO操作
    人生苦短之Python枚举类型enum
    人生苦短之Python类的一二三
    人生苦短之Python装饰器
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/8481916.html
Copyright © 2020-2023  润新知