【codeforces 752F】Santa Clauses and a Soccer Championship

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The country Treeland consists of n cities connected with n - 1 bidirectional roads in such a way that it’s possible to reach every city starting from any other city using these roads. There will be a soccer championship next year, and all participants are Santa Clauses. There are exactly 2k teams from 2k different cities.

During the first stage all teams are divided into k pairs. Teams of each pair play two games against each other: one in the hometown of the first team, and the other in the hometown of the other team. Thus, each of the 2k cities holds exactly one soccer game. However, it’s not decided yet how to divide teams into pairs.

It’s also necessary to choose several cities to settle players in. Organizers tend to use as few cities as possible to settle the teams.

Nobody wants to travel too much during the championship, so if a team plays in cities u and v, it wants to live in one of the cities on the shortest path between u and v (maybe, in u or in v). There is another constraint also: the teams from one pair must live in the same city.

Summarizing, the organizers want to divide 2k teams into pairs and settle them in the minimum possible number of cities m in such a way that teams from each pair live in the same city which lies between their hometowns.

Input
The first line of input contains two integers n and k (2 ≤ n ≤ 2·105, 2 ≤ 2k ≤ n) — the number of cities in Treeland and the number of pairs of teams, respectively.

The following n - 1 lines describe roads in Treeland: each of these lines contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) which mean that there is a road between cities a and b. It’s guaranteed that there is a path between any two cities.

The last line contains 2k distinct integers c1, c2, …, c2k (1 ≤ ci ≤ n), where ci is the hometown of the i-th team. All these numbers are distinct.

Output
The first line of output must contain the only positive integer m which should be equal to the minimum possible number of cities the teams can be settled in.

The second line should contain m distinct numbers d1, d2, …, dm (1 ≤ di ≤ n) denoting the indices of the cities where the teams should be settled.

The k lines should follow, the j-th of them should contain 3 integers uj, vj and xj, where uj and vj are the hometowns of the j-th pair’s teams, and xj is the city they should live in during the tournament. Each of the numbers c1, c2, …, c2k should occur in all uj’s and vj’s exactly once. Each of the numbers xj should belong to {d1, d2, …, dm}.

If there are several possible answers, print any of them.

Example
input
6 2
1 2
1 3
2 4
2 5
3 6
2 5 4 6
output
1
2
5 4 2
6 2 2
Note
In the first test the orginizers can settle all the teams in the city number 2. The way to divide all teams into pairs is not important, since all requirements are satisfied anyway, because the city 2 lies on the shortest path between every two cities from {2, 4, 5, 6}.

【题目链接】:http://codeforces.com/problemset/problem/752/F

【题解】

这里写图片描述
如上图、只要找到这样一个节点就可以了;
它的父亲和儿子们所在的子树->在图中标为①②③…
只要它们的大小size*2都<=k
则这个节点就是符合要求的了;
你可以从这个节点开始dfs一次;
然后把是某个队的hometown的城市加入到一个vecor里面(按dfs序);
则最后for一遍i=1->k/2
v[i]和v[i+k/2]配对就可以了;
因为每个子树的大小都小于等于k/2
则可知v[i+k/2]必然是这个节点的另外一个儿子的子树里面的节点;
则v[i]和v[i+k/2]必然不是在同一个子树里面:
则它们俩链接的时候要经过中间这个节点u;
这样的讨论对所有的i=1->k/2都成立;
则每个配对都会经过这节点u;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int MAXN = 2e5+100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int n,k,macnt[MAXN],cnt[MAXN];
bool team[MAXN];
vector <int> G[MAXN],ans;

void dfs1(int x,int fa)
{
    if (team[x])
        cnt[x]++;
    for (int y:G[x])
    {
        if (y==fa)
            continue;
        dfs1(y,x);
        cnt[x] += cnt[y];
        macnt[x] = max(macnt[x],cnt[y]);
    }
}

void dfs2(int x,int fa)
{
    if (team[x])
        ans.pb(x);
    for (auto y:G[x])
    {
        if (y==fa) continue;
        dfs2(y,x);
    }
}

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    rei(n);rei(k);
    k<<=1;
    rep1(i,1,n-1)
    {
        int x,y;
        rei(x);rei(y);
        G[x].pb(y);G[y].pb(x);
    }
    rep1(i,1,k)
    {
        int x;
        rei(x);
        team[x] = true;
    }
    dfs1(1,-1);
    int u = -1;
    rep1(i,1,n)
        if ( (k-cnt[i])*2 <= k && macnt[i]*2 <= k)
        {
            u = i;
            break;
        }
    puts("1");
    cout << u << endl;
    if (team[u])
        ans.pb(u);
    for (int s : G[u])
        dfs2(s,u);
    rep1(i,0,k/2 -1 )
        printf("%d %d %d\n",ans[i],ans[i+k/2],u);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值