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 ...
随机推荐
- SimpleAliasRegistry
SimpleAliasRegistry :主要使用map最为alias的缓存,并对接口AliasRegister进行实现. ==============================插曲====== ...
- PHPstudy2018 后门简单使用
首先声明,仅为记录使用. 测试用例php-5.4.45 + Apache index.php 使用Firefox 浏览器,可以编辑并且重发http请求 打印出“net user” base64 加密后 ...
- 吴裕雄--天生自然java开发常用类库学习笔记:ListIterator接口
import java.util.ArrayList ; import java.util.List ; import java.util.ListIterator ; public class Li ...
- poj 2456 Aggressive cows 贪心+二分
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25944 Accepted: 11982 ...
- redis简单的实现(java)
1.首先新建一个maven项目,在pom.xml中添加依赖 <dependency> <groupId>redis.clients</groupId> <ar ...
- 对上一篇Logstash的补充
主要补充内容: 1.同步多表 2.配置的参数个别说明 3.elasticsearch的"_id"如果有相同的,那么会覆盖掉,相同"_id"的数据只会剩下最后一条 ...
- P 1008 说反话
转跳点:
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- HTML的文档结构与语法(一)
一.走进Web开发 Web运行的原理: 二.HTML 1.1什么是html HTML是用来描述网页的一种语言 HTML指的是超文本标记语言(Hyper Text Markup Language) 超文 ...
- 二十六、SAP中通过FORMAT COLOR来设置文字背景颜色
一.代码如下 二.效果如下