zoukankan      html  css  js  c++  java
  • 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;
    }
    这条路我们走的太匆忙~拥抱着并不真实的欲望~
  • 相关阅读:
    linux 开发GTK需要的初始配置
    Kernel panic not syncing: cannot execute a PAEenabled kernel on PAEless CPU
    在NetBeans IDE 6.9.1上搭建Android SDK环境(WIN和Linux平台)
    Gtk+ Tutorials & Resources
    新手上路学习配置C,C++,GTK等开发环境
    又是忙碌的一天
    Graphics Programming on linux using C用c语言开发图形界面
    如何在Netbeans下配置Android开发环境 a platform target has to be selected
    linux终端中最漂亮的几款字体介绍及安装
    自已接触过的数据访问方式总结
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2796863.html
Copyright © 2011-2022 走看看