点分治 (等级排) 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\n", root);
    dfs(root, 0);
    for(int i = 1; i <= n; i++) printf("%c%c",ans[i], i==n?'\n':' ');
    return 0;
}

 

转载于:https://www.cnblogs.com/ccut-ry/p/8481916.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值