cf963b Destruction of a Tree
越靠近叶子越优先删掉
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
int n, uu, hea[200005], cnt, deg[200005], fa[200005];
bool vis[200005];
vector<int> shu;
vector<int> ans;
struct Edge{
int too, nxt;
}edge[400005];
void add_edge(int fro, int too){
edge[++cnt].nxt = hea[fro];
edge[cnt].too = too;
hea[fro] = cnt;
}
void dfs(int x, int f){
shu.push_back(x);
fa[x] = f;
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(t!=f) dfs(t, x);
}
}
void shanchu(int x){
vis[x] = true;
ans.push_back(x);
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
deg[t]--;
if(t!=fa[x] && !vis[t]){
if(deg[t]%2==0)
shanchu(t);
}
}
}
int main(){
cin>>n;
for(int i=1; i<=n; i++){
scanf("%d", &uu);
if(!uu) continue;
add_edge(uu, i);
add_edge(i, uu);
deg[i]++; deg[uu]++;
}
dfs(1, 0);
for(int i=shu.size()-1; i>=0; i--){
int x=shu[i];
if(/*vis[x] ||*/deg[x]&1) continue;
shanchu(x);
}
if(ans.size()!=n) printf("NO\n");
else{
printf("YES\n");
for(auto i:ans) printf("%d\n", i);
}
return 0;
}
cf963b Destruction of a Tree的更多相关文章
- CodeForces - 963B Destruction of a Tree (dfs+思维题)
B. Destruction of a Tree time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces963B - Destruction of a Tree
Portal Description 给出一个\(n(n\leq2\times10^5)\)个点的树,每次可以删除一个度数为偶数的点及其相连的边,求一种能够删掉整棵树的方案. Solution 简单起 ...
- codeforces 963B Destruction of a Tree
B. Destruction of a Tree time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 963B:Destruction of a Tree
You are given a tree (a graph with n vertices and n - 1 edges in which it's possible to reach any ve ...
- Codeforces Round #475 (Div. 2) D. Destruction of a Tree
题意:给你一棵树, 只能删度数为偶数的点, 问你能不能将整个图删完, 如果能输入删除的顺序. 思路:对于一棵树来说, 如果里面的点的个数是偶数个则肯定不可能, 偶数个点有奇数条边,而你每次删只能删偶数 ...
- 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 解 对于题目要求,显然一开始的树,要求度数为偶数的节点个数为奇数个,通过奇 ...
- Codeforces 963B Destruction of a Tree 思维+dfs
题目大意: 给出一棵树,每次只能摧毁有偶数个度的节点,摧毁该节点后所有该节点连着的边都摧毁,判断一棵树能否被摧毁,若能,按顺序输出摧毁的点,如果有多种顺序,输出一种即可 基本思路: 1)我一开始自然而 ...
- CodeForces475
A. Splits #include <cstdio> #include <cstdlib> #include <cmath> #include <cstri ...
- Codeforces Round #475 Div. 2 A B C D
A - Splits 题意 将一个正整数拆分成若干个正整数的和,从大到小排下来,与第一个数字相同的数字的个数为这个拆分的权重. 问\(n\)的所有拆分的不同权重可能个数. 思路 全拆成1,然后每次将2 ...
随机推荐
- java的三大特性之一多态概述
多态---概念 所谓多态就是一个引用在不同情况下的多种状态.多态是指通过指向父亲的指针,来调用在不同的子类中实现的方法. 多态---注意事项 00.java允许父类的引用变量引用它的子类的实例(对象) ...
- HTML5标签选择指引
- 超图supermap sdx数据库用sql实现空间查询
在此介绍用sql对超图的空间数据库(sdx)进行空间查询,优点如下: 1.超图推荐的方式是用iobject,此方法要引入iobject 2.超图另一个推荐的方式是用iserver的REST接口,但we ...
- static 关键字用法
static a=0; 就是把a初始化为0:初始值为0而已 即使a是局部变量,每次进入此变量所在的函数,a值还是保持上次赋值: 在中断里建议在局部变量前加上static,以确保此变量值的寿命
- 纪念Google Reader—Google Reader的最后一天
从2006年到今天,几乎每天我都会打开Google Reader,但是今天不一样,因为它是最后一天.心情有些依依不舍,像是与一位多年老朋友永别.因此我非常痛恨Google,先给你送来个好朋友,再从你身 ...
- WinForm 公共控件和属性
Button 按钮 布局 AutoSize 内容超出部分是否扩展到适应尺寸大小 Location 位置坐标 Size 控件大小 行为 Enabled 控件是否启用 visible 控件 ...
- 为Oracle Clusterware修改公用及私有网络接口
出于种种原因我们可能需要为已安装的Oracle集群软件修改其使用的公用或私有网络所使用的网络接口(How to Change Interconnect/Public Interface IP or S ...
- Vector 容器简单介绍
# Vector STL简要介绍 关于STL中的vector容器,以下做一些相关介绍. #### vector 简要概述 vector 称作向量类,属于容器类,实现了动态的数组,用于元素数量变化的对象 ...
- matlab启动
直接在命令行输入matlab会报错 用这两个命令没问题 sudo /usr/local/MATLAB/R2013a/bin/matlab sudo /usr/local/MATLAB/R2013a/b ...
- jQ实现JSON.stringify(obj)方法
jQstringify是使用jQuery实现的JSON.stringify(obj)方法 代码如下:<script type="text/javascript" src=&q ...