• CodeForces 748F Santa Clauses and a Soccer Championship


    题意:有k对队伍,每对队伍之间将举行两次比赛,两支队伍各主办一次。住宿的地方要求在两支队伍家乡的最短路的结点上或者在两支队伍的家乡。问在选择住宿处最少的情况下,怎么组成这k对队伍?

    分析:

    1、因为n个点,n-1条边,且连通图,因此所有队伍的关系形成一棵树。

    2、树上任意两点之间的最短路是唯一的。

    3、因此住宿处一定只有一个,它是k对队伍到彼此家乡的必经结点。

    4、从某点出发,找一个子树中家乡的个数>=k的点作为住宿处,则以该点子树中的家乡为家乡的k支队伍,与其他k支队伍配对即可。

    #pragma comment(linker, "/STACK:102400000, 102400000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define Min(a, b) ((a < b) ? a : b)
    #define Max(a, b) ((a < b) ? b : a)
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 200000 + 10;
    const int MAXT = 1000000 + 10;
    using namespace std;
    int n, k, root;
    vector<int> v[MAXN], l, r;
    int vis[MAXN];
    void init(){
        for(int i = 0; i < MAXN; ++i){
            v[i].clear();
        }
        memset(vis, 0, sizeof vis);
        l.clear();
        r.clear();
        root = 0;
    }
    int get_root(int x, int father){
        int sum = vis[x] ? 1 : 0;
        int len = v[x].size();
        for(int i = 0; i < len; ++i){
            int tmp = v[x][i];
            if(tmp == father) continue;
            sum += get_root(tmp, x);
            if(root) return 0;
        }
        if(sum >= k) root = x;
        return sum;
    }
    void dfs(int x, int father){
        if(vis[x]){
            int L = l.size();
            if(L < k)
                l.push_back(x);
            else r.push_back(x);
        }
        int len = v[x].size();
        for(int i = 0; i < len; ++i){
            int tmp = v[x][i];
            if(tmp == father) continue;
            dfs(tmp, x);
        }
    }
    int main(){
        while(scanf("%d%d", &n, &k) == 2){
            init();
            for(int i = 0; i < n - 1; ++i){
                int a, b;
                scanf("%d%d", &a, &b);
                v[a].push_back(b);
                v[b].push_back(a);
            }
            for(int i = 0; i < 2 * k; ++i){
                int x;
                scanf("%d", &x);
                vis[x] = 1;
            }
            get_root(1, -1);
            dfs(root, -1);
            printf("1\n%d\n", root);
            for(int i = 0; i < k; ++i){
                printf("%d %d %d\n", l[i], r[i], root);
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    [2017BUAA软工助教]结对项目小结
    DPDK flow_filtering 源码阅读
    DPDK flow_classify 源码阅读
    阅读源代码,查出某个宏定义在哪个头文件内的方法
    pktgen-dpdk 实战
    pktgen-dpdk 运行 run.py 报错 Config file 'default' not found 解决方法
    DPDK RX / TX Callbacks 源码阅读
    DPDK skeleton basicfwd 源码阅读
    DPDK helloworld 源码阅读
    DPDK实例程序:testpmd
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6501516.html
Copyright © 2020-2023  润新知