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!".
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.
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!".
4 1 2 1 3 1 4
A B B B
10 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
D C B A D C B D C D
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.
题意:给出一棵树,给每一个点填上一个字母,要求是得任意两个相同字母的点u,v路径上至少有一个点大于这个字母(A最大)
思路:每次选树的重心,给他赋值,最后如果深度大于26,则无解
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
struct node
{
int v,next;
}edge[maxn*2];
int head[maxn],tot;
int N;
int maxvdep;
int vis[maxn];
int size[maxn];
int maxv[maxn];
int ans[maxn];
int root,Max;
void init()
{
tot=0;
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
}
void add_edge(int u,int v)
{
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void dfssize(int u,int f)
{
size[u]=1;
maxv[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(v==f||vis[v])continue;
dfssize(v,u);
size[u]+=size[v];
if(size[v]>maxv[u])maxv[u]=size[v];
}
}
//找重心
void dfsroot(int r,int u,int f)
{
if(size[r]-size[u]>maxv[u])//size[r]-size[u]是u上面部分的树的尺寸,跟u的最大孩子比,找到最大孩子的最小差值节点
maxv[u]=size[r]-size[u];
if(maxv[u]<Max)Max=maxv[u],root=u;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(v==f||vis[v])continue;
dfsroot(r,v,u);
}
}
void dfs(int u,int dep)
{
Max=N;
maxvdep=max(maxvdep,dep);
dfssize(u,0);
dfsroot(u,u,0);
ans[root]=dep;
vis[root]=1;
for(int i=head[root];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!vis[v])
dfs(v,dep+1);
}
}
int main()
{
init();
scanf("%d",&N);
for(int i=1;i<N;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
maxvdep=0;
dfs(1,0);
if(maxvdep>=26)
{
printf("Impossible!\n");
return 0;
}
for(int i=1;i<=N;i++)
printf("%c ",ans[i]+'A');
return 0;
}