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;
}
CodeForces - 748F Santa Clauses and a Soccer Championship的更多相关文章
- 【codeforces 752F】Santa Clauses and a Soccer Championship
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces Round #382 (Div. 2)C. Tennis Championship 动态规划
C. Tennis Championship 题目链接 http://codeforces.com/contest/735/problem/C 题面 Famous Brazil city Rio de ...
- Codeforces 752C - Santa Claus and Robot - [简单思维题]
题目链接:http://codeforces.com/problemset/problem/752/C time limit per test 2 seconds memory limit per t ...
- codeforces 748E Santa Claus and Tangerines
E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Codeforces Round #382 (Div. 2) C. Tennis Championship 斐波那契
C. Tennis Championship time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Codeforces Round #382 (Div. 2) C. Tennis Championship
C. Tennis Championship time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Codeforces 784B Santa Claus and Keyboard Check
题面: 传送门 B. Santa Claus and Keyboard Check Input file: standard input Output file: standard output Time ...
- Codeforces 748D Santa Claus and a Palindrome
雅礼集训期间我好像考完试就开始划水了啊 给出k个长度相同的字符串,每个串有一个权值,选出一些串连成一个回文串.使得选中的串的总权值最大. 如果选一个串,必须同时选一个对称的串.还有一个特殊情况是可以在 ...
- CodeForces 748C Santa Claus and Robot (思维)
题意:给定一个机器人的行走路线,求最少的点能使得机器人可以走这样的路线. 析:每次行走,记录一个方向向量,每次只有是相反方向时,才会增加一个点,最后再加上最后一个点即可. 代码如下: #pragma ...
随机推荐
- 使用 esxcli storage vmfs unmap 命令在精简置备的 LUN 上回收 VMFS 删除的块
官方原文链接: https://kb.vmware.com/s/article/2057513?lang=zh_CN 本文介绍如何通过运行 esxcli storage vmfs unmap 命令回收 ...
- R 对数变换 《回归分析与线性统计模型》page103
BG:在box-cox变换中,当λ = 0时即为对数变换. 当所分析变量的标准差相对于均值而言比较大时,这种变换特别有用.对数据作对数变换常常起到降低数据波动性和减少不对称性的作用..这一变换也能有效 ...
- 2020寒假 05 ——eclipse安装scala环境
在eclipse中安装Scala环境 1安装eclipse插件步骤,点击help,选择Eclipse Marketplace 2.输入Scala,点击go 3.选择搜索到的Scala IDE 4.7. ...
- 八、Vue-lazyload
一.Vue懒加载 文档:https://github.com/hilongjw/vue-lazyload 1.安装 cnpm i vue-lazyload -S 或 npm i vue-lazyloa ...
- POJ 2829 Buy Tickets
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 26443 Accepted: 12639 Des ...
- 学习进度05(billbill长评爬取02)
今天下雪了,是个看<白色相簿2>的好日子. 昨天我们获取所有长评url,今天要解析这些url获取更多的信息随便,点开一个,我们需要的数据有标题,时间,内容.点赞数和评论先不弄了. 解析js ...
- Golang的环境安装
Golang的环境安装 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Go语言环境安装 1>.下载Golang的安装包 博主推荐阅读: https://studygola ...
- 不能安装64位office提示已安装32位如何处理
不能安装64位office提示已安装32位如何处理? 1 2 3 4 5 6 当你遇到提示说[无法安装64位版本的office,以为在您的PC上找一下32位程序......]时如果你还想安装64位 ...
- CAN分帧接收实现
该版本程序实现了上电后先发送MACID检测功能,如果网络上有应答.则一直进行死循环,直到用户更改了本机的ID地址 才可以跳出循环体. 本单片机设置为双滤波 ,使目标地址为0X1F 实现了建立连接命令 ...
- C语言拾遗——strtok
C语言拾遗——strtok 今天刷PAT的时候用到了这个strtok函数,顺手就记录一下 strtok函数包含于头文件string.h 语法:char *strtok( char *str1, con ...