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 ...
随机推荐
- [常用类]Number & Math 类(转载)
下面的表中列出的是 Number & Math 类常用的一些方法: 序号 方法与描述 1 xxxValue() 将 Number 对象转换为xxx数据类型的值并返回. 2 compareTo( ...
- HDU-1873 看病要排队(队列模拟)
看病要排队 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- HTTP协议详解??
HTTP协议: HTTP (hypertext transport protocol) , 即 超 文 本 传 输 协 议 . 这 个 协 议 详 细 规 定 了 浏 览 器 和 万 维 网 服 务 ...
- Python2中range 和xrange的区别??
两者用法相同,不同的是range返回的结果是一个列表,而xrange的结果是一个生成器, 前者是直接开辟一块内存空间来保存列表,后者是边循环边使用,只有使用时才会开辟内存空间, 所以当列表很长时,使用 ...
- poj1011 Sticks (dfs剪枝)
[题目描述] George took sticks of the same length and cut them randomly until all parts became at most 50 ...
- Python学习第四十一天函数装饰器传参数的用法
在不改变函数的结构的基础,我们给函数加新的功能,用是函数装饰器,如果要给函数传递参数,那么应该怎么做呢 @timerdef test2(name,age): time.sleep(3) print(' ...
- TVA金额的计算,以及应该放在那里
标记TTC价格的货物,有以下内容:TTC原价(自动提取),折扣(输入),折扣之后的减价(代金券,或者再次减价),最终TTC单价(自动计算).税率(输入),HT单价(自动计算),单价的税费(也可能不需要 ...
- 关于ftp用户连接时出现500OOPS:can not change directory的解决办法
今天在使用Filezilla连接Linux的时候不能成功,显示"500 OOPS:cannot change directory:/root" 错误,如何解决呢? 默认下是没有开启 ...
- CentOS6.5系统解决中文乱码问题
一. 问题详情 Windows的默认编码为GBK,Linux的默认编码为UTF-8.在Windows下编辑的中文,在Linux下显示为乱码.为了解决此问题,修改Linux的默认编码为GBK. ...
- 在子组件中触发事件,传值给父组件-vue
1.通过$emit触发事件 在子组件<x-test>中触发事件: <button @click="toSearchProduct()">搜索</but ...