# Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) F

189人阅读 评论(0)

solution：

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;

const int maxn = 2E5 + 20;

int n,k,rt,tot,siz[maxn],A[maxn];
bool bo[maxn];

vector <int> v[maxn];

void Dfs1(int x,int fa)
{
siz[x] = bo[x] ? 1 : 0;
for (int i = 0; i < v[x].size(); i++)
{
int to = v[x][i];
if (to == fa) continue;
Dfs1(to,x); siz[x] += siz[to];
}
}

void Dfs2(int x,int fa)
{
bool pass = 0; int Nex;
for (int i = 0; i < v[x].size(); i++)
{
int to = v[x][i];
if (to == fa) continue;
if (siz[to] > k) pass = 1,Nex = to;
}
if (!pass) rt = x; else Dfs2(Nex,x);
}

void Dfs3(int x,int fa)
{
if (bo[x]) A[++tot] = x;
for (int i = 0; i < v[x].size(); i++)
{
int to = v[x][i];
if (to == fa) continue;
Dfs3(to,x);
}
}

int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif

cin >> n >> k;
for (int i = 1; i < n; i++)
{
int x,y; scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
for (int i = 1; i <= 2*k; i++)
{
int x; scanf("%d",&x); bo[x] = 1;
}
Dfs1(1,0); Dfs2(1,0);
if (bo[rt]) A[++tot] = rt;
for (int i = 0; i < v[rt].size(); i++)
Dfs3(v[rt][i],rt);
printf("1\n%d\n",rt);
for (int i = 1; i <= k; i++)
printf("%d %d %d\n",A[i],A[i+k],rt);
return 0;
}

0
0

个人资料
• 访问：201917次
• 积分：8922
• 等级：
• 排名：第2543名
• 原创：733篇
• 转载：1篇
• 译文：0篇
• 评论：16条
文章分类
阅读排行
最新评论