越靠近叶子越优先删掉

#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的更多相关文章

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

  2. Codeforces963B - Destruction of a Tree

    Portal Description 给出一个\(n(n\leq2\times10^5)\)个点的树,每次可以删除一个度数为偶数的点及其相连的边,求一种能够删掉整棵树的方案. Solution 简单起 ...

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

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

  5. Codeforces Round #475 (Div. 2) D. Destruction of a Tree

    题意:给你一棵树, 只能删度数为偶数的点, 问你能不能将整个图删完, 如果能输入删除的顺序. 思路:对于一棵树来说, 如果里面的点的个数是偶数个则肯定不可能, 偶数个点有奇数条边,而你每次删只能删偶数 ...

  6. 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 解 对于题目要求,显然一开始的树,要求度数为偶数的节点个数为奇数个,通过奇 ...

  7. Codeforces 963B Destruction of a Tree 思维+dfs

    题目大意: 给出一棵树,每次只能摧毁有偶数个度的节点,摧毁该节点后所有该节点连着的边都摧毁,判断一棵树能否被摧毁,若能,按顺序输出摧毁的点,如果有多种顺序,输出一种即可 基本思路: 1)我一开始自然而 ...

  8. CodeForces475

    A. Splits #include <cstdio> #include <cstdlib> #include <cmath> #include <cstri ...

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

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

随机推荐

  1. GNUPG

    PGP (Pretty Good Privacy) 是由 Phil Zimmermann 于 1991 开发的一个用于数据加密和数字签名的程序,由于被广泛应用以至于后来形成一个开放的标准 OpenPG ...

  2. js基础的自定义属性练习

    js基础的自定义属性练习: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type ...

  3. Ubuntu 16.04 server版本开机启动脚本不支持

    Ubuntu16.04开机启动的脚本一直不支持,错误用在将开机启动脚本放到了home/usr/的目录下,应该放到/root才能正常启动.#!/bin/sh -e ## rc.local## This ...

  4. SQL Server数据库所有表重建索引

    USE My_Database;DECLARE @name varchar(100) DECLARE authors_cursor CURSOR FOR  Select [name]   from s ...

  5. Python 中 创建类方法为什么要加self

    Python的类方法和普通的函数有一个明显的区别,在类的方法必须有一个额外的第一个参数(self),但在调用这个方法的时候不必为这个参数数值(显胜于隐的引发).在Python的类方法中这个特别的参数指 ...

  6. ZOJ 3471 Most Powerful (状压DP,经典)

    题意: 有n个原子,每当两个原子碰撞时就会产生能量,并且消耗其中一个原子.已知每两个原子碰撞时消耗其中指定一个原子所产生的能量,问最多能产生多少能量? 思路: 一开始以为是找一个有序序列,使得能量最大 ...

  7. UESTC 1307 WINDY数 (数位DP,基础)

    题意: windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数.windy想知道,在A和B之间,包括A和B,总共有多少个windy数? 思路: 就是给连续的两 ...

  8. 【Python图像特征的音乐序列生成】生成伴奏旋律(附部分代码)

    做了半天做的都是一些细枝末节的东西,嗨呀. 伴奏旋律是Ukulele和弦,MIDI发音乐器是Guitar.在弹唱的时候,Ukulele和弦就是伴奏. 我们以创建<成都>伴奏为例: 节奏型: ...

  9. C#注册表操作类(完整版)

    下面贴出自己用C#写的注册表操作类,欢迎大家拍砖! 1.注册表基项静态域 1 /// <summary> 2 /// 注册表基项静态域 3 /// 4 /// 主要包括: 5 /// 1. ...

  10. JAVA小游戏之两个物体碰撞产生的碰撞检测

    首先必须了解两个物体,在移动时,会有怎样的效果,比如沪我们小时候耍过的坦克大战.看起来很简单,但是写起代码来,复杂的要多: 下面举个例子: // 构造一个新的 Rectangle,其左上角的坐标为 ( ...