zoukankan      html  css  js  c++  java
  • 树上的构造 树分治+树重心的性质 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
    ")
    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
  • 相关阅读:
    BSF、BSR: 位扫描指令
    驱动学习4
    DDK Build的DIRS和SOURCE文件
    sql语句中的字符串拼接
    delphi中WMI的使用(二)
    delphi中WMI的使用(一)
    WPF中实现砖块拖动的方法(2)
    HttpWebRequest中UserAgent的使用
    获取本机外网IP相关
    WPF中实现砖块拖动的方法(1)
  • 原文地址:https://www.cnblogs.com/heimao5027/p/6648513.html
Copyright © 2011-2022 走看看