题意:给你一棵树, 只能删度数为偶数的点, 问你能不能将整个图删完, 如果能输入删除的顺序。

思路:对于一棵树来说, 如果里面的点的个数是偶数个则肯定不可能, 偶数个点有奇数条边,而你每次删只能删偶数条边。

那么我们对于每个父亲儿子来说, 如果儿子的子树的大小为奇数, 那么肯定先删父亲, 反之先删儿子, 建立关系图, 跑一遍拓扑序就好啦。

 #include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int,int>
#define ull unsigned long long
using namespace std; const int N=1e6+;
const int M=+;
const int inf=0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9 + ; vector<int> edge[N], e[N], ans;
int n, cnt[N], deg[N];
int vis[N];
void dfs(int u ,int pre) {
cnt[u] = ;
for(int v : edge[u]) {
if(v == pre) continue;
dfs(v, u);
cnt[u] += cnt[v];
}
} bool dfs2(int u) {
vis[u] = -;
for(int v : e[u]) {
if(vis[v] == -)
return false;
if(!vis[v] && !dfs2(v))
return false;
}
vis[u] = ;
ans.push_back(u);
return true;
} void dfs3(int u, int pre) {
for(int v : edge[u]) {
if(v == pre) continue;
if(cnt[v] % ) {
e[u].push_back(v);
deg[v]++;
} else {
e[v].push_back(u);
deg[u]++;
}
dfs3(v, u);
}
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) {
int fa; scanf("%d", &fa);
if(fa) {
edge[fa].push_back(i);
edge[i].push_back(fa);
}
}
if(n % == ) {
puts("NO");
return ;
} dfs(, );
dfs3(, ); for(int i = ; i <= n; i++) {
if(vis[i]) continue;
if(!dfs2(i)) {
puts("NO");
return ;
}
}
puts("YES");
for(int i = ans.size() - ; i >= ; i--)
printf("%d\n", ans[i]);
return ;
}
/*
*/

Codeforces Round #475 (Div. 2) D. Destruction of a Tree的更多相关文章

  1. Codeforces Round #475 Div. 2 A B C D

    A - Splits 题意 将一个正整数拆分成若干个正整数的和,从大到小排下来,与第一个数字相同的数字的个数为这个拆分的权重. 问\(n\)的所有拆分的不同权重可能个数. 思路 全拆成1,然后每次将2 ...

  2. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)

    A. Alternating Sum 就是个等比数列,特判公比为 $1$ 的情况即可. #include <bits/stdc++.h> using namespace std; ; ; ...

  3. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1) 963B 964D B Destruction of a Tree

    题 OvO http://codeforces.com/contest/963/problem/B CF 963B 964D 解 对于题目要求,显然一开始的树,要求度数为偶数的节点个数为奇数个,通过奇 ...

  4. Codeforces Round #475 Div. 1

    B:当n是偶数时无解,因为此时树中有奇数条边,而我们每次都只能删除偶数条.当n是奇数时一定有解,因为此时不可能所有点度数都为奇数,只要找到一个度数为偶数的点,满足将它删掉后,各连通块大小都为奇数就可以 ...

  5. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)D. Frequency of String

    题意:有一个串s,n个串模式串t,问s的子串中长度最小的包含t k次的长度是多少 题解:把所有t建ac自动机,把s在ac自动机上匹配.保存每个模式串在s中出现的位置.这里由于t两两不同最多只有xsqr ...

  6. Codeforces Round #475 (Div. 2) C - Alternating Sum

    等比数列求和一定要分类讨论!!!!!!!!!!!! #include<bits/stdc++.h> #define LL long long #define fi first #defin ...

  7. Codeforces Round #475 (Div. 2)

    B. Messages 题意:有n个消息分别在ti的时候收到.设所有消息收到时初始值为A,每过一秒,其值减去B.当在某一秒选择读某个消息时,获值为当前消息的值:如果在某一秒结束的时候,手上有k则消息未 ...

  8. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2)

    A. Splits time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  9. Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和

    B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...

随机推荐

  1. c# 新建文本文件、遍历读取文本、删除文本行

    如果该物理路径没有该文本则创建一个新文本 if (!File.Exists(@"C:\db.txt")){FileStream fs = new FileStream(@" ...

  2. 32个Python爬虫实战项目,满足你的项目慌

    爬虫项目名称及简介 一些项目名称涉及企业名词,小编用拼写代替 1.[WechatSogou]- weixin公众号爬虫.基于weixin公众号爬虫接口,可以扩展成其他搜索引擎的爬虫,返回结果是列表,每 ...

  3. bzoj1040 骑士

    Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火 ...

  4. Python 使用multiprocessingm模块创建多进程

    from multiprocessing import Process def run_proc(name): print("子进程Process %s(%s)运行..."%(na ...

  5. 解题:HNOI 2013 Cards

    题面 除了不洗牌以外,每种洗牌方式的每个循环里的颜色必须一样,然后大力背包一下就好了.最后记得把不洗牌的方案也算进去 #include<cstdio> #include<cstrin ...

  6. bug5 Debug:This kind of launch is configured to openthe debug perspective when it解决办法

    启动tomcat时,myeclipse报错: This kind of launch is configured to openthe debug perspective when itsuspend ...

  7. 无焦点下获取条码枪返回值的Hook(再次改良版)

    针对上一个版本再次改良,上除掉无用代码,新手绝对可以看懂! using System; using System.Collections.Generic; using System.Linq; usi ...

  8. SQL记录-PLSQL字符串

    PL/SQL字符串 PL/SQL字符串实际上是一个可选的尺寸规格字符序列.字符可以是数字,字母,空白,特殊字符或全部的组合. PL/SQL提供了三种类型的字符串: 固定长度字符串:在这样的字符串,程序 ...

  9. BFS简单题套路_Codevs 1215 迷宫

    BFS 简单题套路 1. 遇到迷宫之类的简单题,有什么行走方向的,先写下面的 声明 ; struct Status { int r, c; Status(, ) : r(r), c(c) {} // ...

  10. Windows系统安装————windows7 企业版 无法安装 NET.framework4.52-4.6版本在WIN7下解决办法

    官方安装包下载地址:https://www.microsoft.com/zh-cn/download/details.aspx?id=48137 我安装了NMM后提示NET.framework版本太低 ...