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 ...
随机推荐
- redis cluster 添加/删除节点操作
RedisCluster 添加/删除节点 添加节点新配置两个测试节点8008和9009 [root@--- ~]# /usr/local/redis-/bin/redis-server /u02/re ...
- 用JS写一个网站树形菜单
先上效果图: 主体内容就是侧边展示的一二三级菜单,树形结构的. 前端页面布局内容,页面内容简单用ul li 来完成所有的罗列项.用先后顺序来区分一级二级三级: <body> <b&g ...
- 通过流量管理器和 Azure Functions(作为代理)为全球用户提供最靠近的认知服务(或自定义API)
本实战是一个中等复杂度的综合性实战,涉及到的内容有TrafficManager,AzureFunctions,域名/域名解析等几个内容. 本案例基础介绍: https://www.bilibili.c ...
- 用ftp命令实现主机文件批量更新
我们的主机环境是windows 2003,平时程序员访问都喜欢用远程桌面.简单快捷直观.不过我比较喜欢在本地用vim和命令行,这样编辑修改不需要受网络影响. 这种情况下,我本地调试的程序,要经常更新到 ...
- cmd命令打开本地*.db数据文件的一些坑
昨天刚看了下sqlite数据库,用的是cmd窗口 写的,建了几个表,今天在次打开,发现.问题有点小多啊.. 我也不知道我的数据库名字后面为啥会带 (“ : ”) 下面是我的数据文件: 刚开始看了下, ...
- NO14 快照-克隆-必须掌握的Linux目录结构
壹 VMware克隆,快照讲解及相应问题讲解: ·快照:比喻:假设把人生作一个快照.1岁10岁20岁6无限还原到前一个设置的节点. ·克隆学习一般用链接克隆,不另外占用磁盘,但是依赖本体虚拟机.完整 ...
- Linux应用可通过USB访问Android设备-Chrome OS 75版发布
导读 谷歌已经为支持的Chromebook设备发布了Chrome OS 75操作系统,这是一个主要版本,增加了各种新功能,最新安全补丁和其他改进. 对于大多数Chromebook设备,Chrome O ...
- 图片字节流生成bmp文件
1 BITMAPFILEHEADER bfh;//文件头 2 bfh.bfType=0x4d42; bfh.bfOffBits=sizeof(bfh)+sizeof(BITMAPINFOHEADER) ...
- MongoDB 常用查询语法
一.查询 find方法 db.collection_name.find(); 查询所有的结果: select * from users; db.users.find(); 指定返回那些列(键): se ...
- Java8集合框架——LinkedHashMap源码分析
本文的结构如下: 一.LinkedHashMap 的 Javadoc 文档注释和简要说明 二.LinkedHashMap 的内部实现:一些扩展属性和构造函数 三.LinkedHashMap 的 put ...