思路:

本质是求一个树上的最大匹配能否覆盖所有的点。

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(树上最大匹配)的更多相关文章

  1. Codeforces Gym 102392F Game on a Tree (SEERC2019 F题) 题解

    题目链接:https://codeforces.com/gym/102392/problem/F 题意:被这题题意坑了很久,大意是说有一棵根为 \(1\) 的树,每个节点初始都是白色, \(Alice ...

  2. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...

  3. 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 ...

  4. 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 ...

  5. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. 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 ...

  7. HDU - 6178:Monkeys (贪心&树上最大匹配输&输入优化)

    There is a tree having N vertices. In the tree there are K monkeys (K <= N). A vertex can be occu ...

  8. Codeforces 570D - Tree Requests(树上启发式合并)

    570D - Tree Requests 题意 给出一棵树,每个节点上有字母,查询 u k,问以 u 为根节点的子树下,深度为 k 的所有子节点上的字母经过任意排列是否能构成回文串. 分析 一个数组 ...

  9. 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 ...

随机推荐

  1. [常用类]StringBuffer 类,以及 StringBuilder 类

    线程安全,可变的字符序列. 字符串缓冲区就像一个String ,但可以修改. 在任何时间点,它包含一些特定的字符序列,但可以通过某些方法调用来更改序列的长度和内容. 字符串缓冲区可以安全地被多个线程使 ...

  2. Appium+Python之PO模型(Page object Model)

    思考:我们进行自动化测试时,如果把代码都写在一个脚本中,代码的可读性会变差,且后期代码维护也麻烦,最好的想法就是测试对象和测试用例可以分离,可以很快定位问题,代码可读性高,也比较容易理解.这里推荐大家 ...

  3. Log4Net 之走进Log4Net (四)

    原文:Log4Net 之走进Log4Net (四) 一.Log4net的结构 log4net 有四种主要的组件,分别是Logger(记录器), Repository(库), Appender(附着器) ...

  4. 获取请求头中User-Agent工具类

    public class AgentUserKit { private static String pattern = "^Mozilla/\\d\\.\\d\\s+\\(+.+?\\)&q ...

  5. SwiftUI 实战:从 0 到 1 研发一个 App

    心得感悟 起初看到 WWDC 上的演示 SwiftUI 时,我就觉得 SwiftUI 有种陌生的熟悉感(声明式语法),所以体验下,看看有没有什么启发. 先说下整体项目完成下来的感受: 用 Swift ...

  6. basename 显示文件名或目录名

    1. 命令功能 basename 显示文件名或目录名,不显示文件的全路径文件名 2. 语法格式 basename  文件路径名 3. 使用范例 [root@localhost data]# basen ...

  7. vue的class和style的绑定

    <div class="input-search" :class="{input-search-focus : iscur == 1}"> 在原本有 ...

  8. bzoj2325 [ZJOI2011]道馆之战 树链剖分+DP+类线段树最大字段和

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2325 题解 可以参考线段树动态维护最大子段和的做法. 对于线段树上每个节点 \(o\),维护 ...

  9. 在PHPstorm上安装thinkPHP

    >环境:ubuntu php7.2 phpstorm https://blog.csdn.net/roukmanx/article/details/85646174 https://www.ka ...

  10. mysql 5.6 datetime default now()

    CREATE TABLE `test` (   id int,   `gmt_create` datetime  DEFAULT NOW() not NULL )ENGINE=InnoDB; mysq ...