树上的构造 树分治+树重心的性质 Codeforces Round #190 (Div. 2) E

解决一道算法题目,要求在树形结构中合理分配不同等级的军官,确保任意两个相同等级的军官间路径上有更高级别的军官。采用重心分解的方法来确定最优分配方案。

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

 

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

Examples
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.

 

 

 

题目大意:题意:给出一棵树,给每一个点填上一个字母,要求是得任意两个相同字母的点u,v路径上至少有一个点大于这个字母(A最大)

思路:

'A'节点必然只有一个,且他的位置放在重心一定是最优的。(证明利用反证法证明)

然后我们就每次找重心即可。

(md我好菜啊,又不会构造)

 

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1e5 + 5;
int n;
vector<int> G[maxn];
int val[maxn], sz[maxn];
bool vis[maxn];

void dfs_sz(int u, int fa){
    sz[u] = 1;
    for (int i = 0; i < G[u].size(); i++){
        int v = G[u][i];
        if (v == fa || vis[v]) continue;
        dfs_sz(v, u);
        sz[u] += sz[v];
    }
}

void dfs_ce(int u, int fa, int &cetroid, int &maxcnt, int allcnt){
    int tmp = allcnt - sz[u];
    for (int i = 0; i < G[u].size(); i++){
        int v = G[u][i];
        if (v == fa || vis[v]) continue;
        dfs_ce(v, u, cetroid, maxcnt, allcnt);
        tmp = max(tmp, sz[v]);
    }
    if (tmp < maxcnt) {cetroid = u, maxcnt = tmp;}
}

void dfs(int u, int deep){
    int cetroid, maxcnt = maxn * 100;
    dfs_sz(u, -1);
    dfs_ce(u, -1, cetroid, maxcnt, sz[u]);
    val[cetroid] = deep;
    vis[cetroid] = true;
    for (int i = 0; i < G[cetroid].size(); i++){
        int v = G[cetroid][i];
        if(vis[v]) continue;
        dfs(v, deep + 1);
    }
    vis[cetroid] = false;
}

bool solve(){
    dfs(1, 1);
    for (int i = 1; i <= n; i++){
        if (val[i] > 26) return false;
    }
    for (int i = 1; i <= n; i++){
        val[i]--;
        printf("%c ", val[i] + 'A');
    }
    cout << endl;
    return true;
}

int main(){
    cin >> n;
    for (int i = 1; i < n; i++){
        int u, v; scanf("%d%d", &u, &v);
        G[u].pb(v), G[v].pb(u);
    }
    if (!solve()) puts("Impossible!");
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/heimao5027/p/6648513.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值