• ACdream群赛(4) B Double Kings


    Problem B: Double Kings

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 142  Solved: 65
    [Submit][Status][Web Board]

    Description

    Our country is tree-like structure, that is to say that N cities is connected by exactly N - 1 roads.
    The old king has two little sons. To make everything fairly, he dicided to divide the country into two parts and each son get one part. Two sons can choose one city as their capitals. For each city, who will be their king is all depend on whose capital is more close to them. If two capitals have the same distance to them, they will choose the elder son as their king. 
    (The distance is the number of roads between two city)
    The old king like his elder son more, so the elder son could choose his capital firstly. Everybody is selfish, the elder son want to own more cities after the little son choose capital while the little son also want to own the cities as much as he can.
    If two sons both use optimal strategy, we wonder how many cities will choose elder son as their king.

    Input

    There are multiple test cases.
    The first line contains an integer N. (1 <= N <= 50000)
    The next N - 1 lines each line contains two integers a and b indicating there is a road between city a and city b. (1 <= a, b <= N)

    Output

    For each test case, output an integer indicating the number of cities who will choose elder son as their king.

    Sample Input

    4 1 2 2 3 3 4 4 1 2 1 3 1 4

    Sample Output

    2 3

    HINT

    分析:开始可用无向图保存,然后任意取个点为总树根,从该根开始建树,没访问一个节点,就标记他已经是根,不会再成为其他的儿子。每个节点保存的是该点以下的所有节点,也可以理解为对该树的一种划分方式。最后扫每个节点,找到划分最均衡的方式,也就是小的那部分最大的方式。

    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<algorithm>
    using namespace std;
    int deg[50010];
    vector<int> adj[50010];
    int ans, gen[50010];
    
    void dfs(int a) {
        gen[a] = 1;
        int i,b;
        deg[a] = 1;
        for (i = 0; i < adj[a].size(); ++i) {
            b=adj[a][i];
            if (!gen[b]) {
                dfs(b);
                deg[a] += deg[b];
            }
        }
    }
    
    int main() {
        int n, i, a, b, bj, t;
        while (scanf("%d", &n) != EOF) {
            bj = n / 2;
            for (i = 1; i <= n; ++i)
                adj[i].clear();
            memset(gen, 0, sizeof (gen));
            for (i = 0; i < n - 1; ++i) {
                scanf("%d%d", &a, &b);
                adj[a].push_back(b);
                adj[b].push_back(a);
            }
    
            dfs(1);
            ans = -1;
            for (i = 1; i <= n; ++i) {
                if (deg[i] > bj) {
                    deg[i] = n - deg[i];
                }
                if (deg[i] > ans)
                    ans = deg[i];
            }
            printf("%d\n", n - ans);
        }
        return 0;
    }
    这条路我们走的太匆忙~拥抱着并不真实的欲望~
  • 相关阅读:
    ege图形化编程配置过程及出现的问题解决方法
    两个头文件相互包含导致未定义类型
    20180318CSP比赛
    jdk7和jdk8都下载了 如何设置java版本为jdk7?
    CCF|CSP|模拟试题|游戏
    2018蓝桥杯|基础练习|十六进制转八进制
    2018蓝桥杯|历届试题|翻硬币
    2018蓝桥杯|历届试题|数字游戏
    2020年9月18日 可变字符序列:StringBuffer和StringBuilder(尽量掌握底层代码跟踪分析的能力)
    2020年9月17日 String 常用方法四、五、六、七、八、九
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2796863.html
Copyright © 2020-2023  润新知