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 ...
随机推荐
- XmlBeanDefinitionReader
- STM32 MacOS开发
CLion + STM32CubeMX + STLINK 安装CLion jetbrain官网 汉化补丁 安装homebrew ...略 安装STlink命令工具 $ brew install stl ...
- stl_map复习
set和map的底层模板是红黑树,可以有不同的键值和实值,关于增删改查,迭代器的使用都在代码里面,亲手尝试更方便记忆 #include <iostream>#include <map ...
- 春运到了,带你用python来抢票回家!
不知不觉,一年一度的春运抢票大幕已经拉开,想快速抢到回家的车票吗?作为程序员,这些技术手段,你一定要知道. 为了让大家更快捷更便利的抢火车票,各种各样的抢票软件应需而生,这类软件大部分都是付费抢票的机 ...
- centos7下安装ansible
由于centos7预装了python,因此我们可以跳过python的安装环节(记得关闭防火墙) [root@model ~]# [root@model ~]# python --version Pyt ...
- ACM-Antiprime数
问题描述: swust打不开,随便找了个博客.... 对于任何正整数x,起约数的个数记做g(x).例如g(1)=1,g(6)=4. 定义:如果某个正整数x满足:对于任意i(0<i<x) ...
- Python基础week1
本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...
- Windows 下 GNS3 安装与基本使用指南
1.GNS3简介 GNS3是一款图形化的网络虚拟软件,可以运行在多个平台(windows,linux,mac OS).我们可以通过它来学习Cisco的认证,或者是检验将要在生产环境中部署实施的相关配置 ...
- Easy_vb
拿到之后运行一下 之后使用ida打开先关键字搜索一下,结果就出来了
- Spring课程 Spring入门篇 7-3 advice扩展
课程链接: 1 解析 1.1 advice中aspect 切面传参 1.2 通知参数名称——argNames属性, 参数为 JoinPoint.ProceedingJoinPoint.JoinPoin ...