Codeforces Round #190 (Div. 2) E. Ciel the Commander 点分治

E. Ciel the Commander

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://www.codeforces.com/contest/322/problem/E

Description

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!".

Sample Input

4
1 2
1 3
1 4

Sample Output

A B B B

HINT

 

题意

 给你一棵树,然后让你给每一个顶点标等级,使得每一个相同等级的顶点的简单路径之间,必定存在一个顶点等级比他们两个低

等级从A-Z,一共26种

题解:

采用树分治做,每次找到树的重心。

为什么找重心呢?我们从一条链的情况上来看,肯定越小的放在越重心越好,然后利用这个性质不停分治就行了

代码:

#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
#define maxn 100005
int vis[maxn],son[maxn],f[maxn],sum,root,ans[maxn];
vector<int> E[maxn];
void getroot(int x,int fa)
{
    son[x]=1;f[x]=0;
    for(int i=0;i<E[x].size();i++)
    {
        int v = E[x][i];
        if(v == fa || vis[v])continue;
        getroot(v,x);
        son[x]+=son[v];
        f[x]=max(f[x],son[v]);
    }
    f[x]=max(f[x],sum-son[x]);
    if(f[x]<f[root])root=x;
}
void work(int x,int fa,int dep)
{
    ans[x]=dep;
    vis[x]=1;
    for(int i=0;i<E[x].size();i++)
    {
        int v = E[x][i];
        if(vis[v])continue;
        sum=son[v],root=0;
        getroot(v,x);
        work(root,x,dep+1);
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        int x,y;scanf("%d%d",&x,&y);
        E[x].push_back(y);
        E[y].push_back(x);
    }
    f[0]=sum=n;
    getroot(1,0);
    work(root,0,0);
    for(int i=1;i<=n;i++)
        printf("%c ",ans[i]+'A');
    printf("\n");
    return 0;
}

 

posted @ 2015-12-07 19:58  qscqesze  阅读(369)  评论(0编辑  收藏  举报