CF 321C(Ciel the Commander-点分治)

C. Ciel the Commander
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
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.



考试时完全没思路(构造题?)

结果果真是构造

方法:每次选择一个点,然后填上最大的那个数,之后删掉它,递归做其它的子树

正确性显然,我们希望它深度≤26

点分治++

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100000+10)
#define MAXM (200000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
int n;
int edge[MAXM],next[MAXM]={0},pre[MAXN]={0},size=0;
void addedge(int u,int v)
{
    edge[++size]=v;
    next[size]=pre[u];
    pre[u]=size;
}
void addedge2(int u,int v){addedge(u,v),addedge(v,u);}
int opt[MAXN],siz[MAXN],val[MAXN]={0},all[MAXN],tot=0;
void dfs(int x,int fa)
{
    siz[x]=1;opt[x]=0;all[++tot]=x;
    Forp(x)
    {
        int &v=edge[p];
        if (v^fa&&!val[v])
        {
            dfs(v,x);siz[x]+=siz[v];
            opt[x]=max(opt[x],siz[v]);
        }
    }
}
void solve(int root,int l)
{
    tot=0,dfs(root,0);
    int minopt=INF,minoptx=0;
    For(i,tot)
    {
        int u=all[i];
        opt[u]=max(opt[u],tot-siz[u]);
        if (minopt>opt[u]) minopt=opt[u],minoptx=u;
    }
    val[root=minoptx]=l;
    Forp(root)
    {
        int &v=edge[p];
        if (!val[v]) solve(v,l+1);
    }

}
int main()
{
//	freopen("Commander.in","r",stdin);
    cin>>n;
    For(i,n-1)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        addedge2(u,v);
    }
    solve(1,1);
    For(i,n) if (val[i]>26) {cout<<"Impossible!"<<endl;return 0;}
    For(i,n-1) cout<<(char)(val[i]+'A'-1)<<' ';
    cout<<(char)(val[n]+'A'-1)<<endl;
	return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值