Game on a Tree Gym - 102392F(树上最大匹配)
思路:
本质是求一个树上的最大匹配能否覆盖所有的点。
dfs遍历,用qian[]数组记录当前节点的子树内有几个没有匹配的点(初始化为-1因为可以匹配掉一个子树中未匹配的点),pipei[]数组记录当前节点是否匹配。如果一个点u的子节点有未匹配的,那么u可以匹配掉一个点,但是有多个未匹配的点,就得累积在u上。也就是说如果qian[]数组>0,那么子树中有未匹配的点,需要将该子树的根u标记为未匹配状态,使得u的父亲fu能知道u中有未匹配的点,从而使fu可以匹配。
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
vector<int>E[maxn];
bool pipei[maxn];
int qian[maxn];
void dfs(int u,int fu){
qian[u]=-1;
int cnt=0;
for(auto v:E[u]){
if(v==fu)continue;
dfs(v,u);
qian[u]+=max(qian[v],pipei[v]?0:1);
if(!pipei[v])cnt++;
}
if(!cnt||qian[u]>0)pipei[u]=0;
else pipei[u]=1;
return;
}
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
E[u].push_back(v);
E[v].push_back(u);
}
dfs(1,0);
if(qian[1]!=0){
printf("Alice\n");
}
else printf("Bob\n");
}
Game on a Tree Gym - 102392F(树上最大匹配)的更多相关文章
- Codeforces Gym 102392F Game on a Tree (SEERC2019 F题) 题解
题目链接:https://codeforces.com/gym/102392/problem/F 题意:被这题题意坑了很久,大意是说有一棵根为 \(1\) 的树,每个节点初始都是白色, \(Alice ...
- 【SPOJ】Count On A Tree II(树上莫队)
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...
- SPOJ 10628 COT - Count on a tree(在树上建立主席树)(LCA)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- COT2 - Count on a tree II(树上莫队)
COT2 - Count on a tree II You are given a tree with N nodes. The tree nodes are numbered from 1 to N ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- SPOJ COT2 Count on a tree II(树上莫队)
题目链接:http://www.spoj.com/problems/COT2/ You are given a tree with N nodes.The tree nodes are numbere ...
- HDU - 6178:Monkeys (贪心&树上最大匹配输&输入优化)
There is a tree having N vertices. In the tree there are K monkeys (K <= N). A vertex can be occu ...
- Codeforces 570D - Tree Requests(树上启发式合并)
570D - Tree Requests 题意 给出一棵树,每个节点上有字母,查询 u k,问以 u 为根节点的子树下,深度为 k 的所有子节点上的字母经过任意排列是否能构成回文串. 分析 一个数组 ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路
题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...
随机推荐
- AtCoder Beginner Contest 133 B - Good Distance
地址:https://atcoder.jp/contests/abc133/tasks/abc133_b 核心问题:判断一个浮点数开方是否为整数 ; double ans1=sqrt(ans); if ...
- 51nod - 1586 - 约数和 - 打表 - 思维
https://www.51nod.com/Challenge/Problem.html#problemId=1586 一眼看过去居然一点思路都没有的,一言不合就打表,打贡献表. #include & ...
- SSH简单Demo
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="3 ...
- css盒子模型之边框宽度,边框颜色与边框样式
/* width和height只是设置盒子内容区的大小,而不是盒子的整个大小, 盒子可见框的大小由内容区,内边距和边框共同决定. */ .box1 { /* 设置内容区的宽度为400px */ wid ...
- rocketmq-console控制台管理界面配置
Rocketmq可视化管理控制台配置 前提: RocketMQ有一个对其扩展的开源项目incubator-rocketmq-externals,这个项目中有一个子模块叫“rocketmq-consol ...
- docker安装es
下载镜像 docker pull docker.elastic.co/elasticsearch/elasticsearch:6.8.1 创建容器并映射docker run -e ES_JAVA_OP ...
- 【Kickstart】2018 Round (Practice ~ C)
Practice Round Problem A GBus count (9pt/15pt) (2019年1月14日,kickstart群每日一题) 题意:有一条笔直的大路,上面有城市编号从 1 开始 ...
- CSS3 nth-of-type(n)选择器 last-of-type选择器 nth-last-of-type(n)选择器 CSS3 only-child选择器 only-of-type选择器
CSS3 nth-of-type(n)选择器 “:nth-of-type(n)”选择器和“:nth-child(n)”选择器非常类似,不同的是它只计算父元素中指定的某种类型的子元素.当某个元素中的子元 ...
- jmeter性能工具 之 传参 (三)
jmeter 主要有三种方式:键值对传参,json格式传参,外部传参 1.键值对传参 可以参考上篇登陆,使用的传参方式是键值对传参 2.json 格式传参 用json 格式传参不要忘了加http 头 ...
- Beats:如何创建一个定制的Elastic Beat
Beats作为Elastic Stack家族中重要的部分.它可以和方便地让我们把我们的数据发送到Elasticsearch或Logstash之中.如果我们想要生成自己的Beat,请使用GitHub的b ...