CodeForces 321C Ciel the Commander

本文介绍了一种基于树分治的算法,用于解决特定条件下军官分配的问题。在给定的城市和连接这些城市的道路中,每个城市需分配一个等级的军官,并确保同一等级的军官之间存在更高级别的监视。通过深度优先搜索和寻找根节点实现有效分配。

Ciel the Commander

Time Limit: 1000ms
Memory Limit: 262144KB
This problem will be judged on  CodeForces. Original ID: 321C
64-bit integer IO format: %I64d      Java class name: (Any)
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 xand 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

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

Hint

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.

 

Source

 
解题:树分治
深度大于26,说明impossible
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 100010;
 4 bool done[maxn];
 5 vector<int>g[maxn];
 6 char ans[maxn];
 7 int sz[maxn],maxson[maxn];
 8 int dfs(int u,int fa){
 9     sz[u] = 1;
10     maxson[u] = 0;
11     for(int i = g[u].size()-1; i >= 0; --i){
12         if(g[u][i] == fa || done[g[u][i]]) continue;
13         dfs(g[u][i],u);
14         sz[u] += sz[g[u][i]];
15         maxson[u] = max(maxson[u],sz[g[u][i]]);
16     }
17     return sz[u];
18 }
19 int FindRoot(const int sum,int u,int fa){
20     int ret = u;
21     maxson[u] = max(maxson[u],sum - sz[u]);
22     for(int i = g[u].size()-1; i >= 0; --i){
23         if(g[u][i] == fa || done[g[u][i]]) continue;
24         int x = FindRoot(sum,g[u][i],u);
25         if(maxson[x] < maxson[ret]) ret = x;
26     }
27     return ret;
28 }
29 bool solve(int u,char ch){
30     int root = FindRoot(dfs(u,0),u,0);
31     done[root] = true;
32     ans[root] = ch;
33     if(ch > 'Z') return false;
34     for(int i = g[root].size()-1; i >= 0; --i){
35         if(done[g[root][i]]) continue;
36         if(!solve(g[root][i],ch + 1)) return false;
37     }
38     return true;
39 }
40 int main(){
41     int n,u,v;
42     while(~scanf("%d",&n)){
43         for(int i = 0; i <= n; ++i){
44             done[i] = false;
45             g[i].clear();
46         }
47         for(int i = 1; i < n; ++i){
48             scanf("%d%d",&u,&v);
49             g[u].push_back(v);
50             g[v].push_back(u);
51         }
52         if(solve(1,'A'))
53         for(int i = 1; i <= n; ++i)
54             printf("%c%c",ans[i],i == n?'\n':' ');
55         else puts("Impossible!");
56     }
57     return 0;
58 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4950886.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值