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 ...
随机推荐
- fastjson格式化输出内容
引入fastjson <!--fastjson--><dependency> <groupId>com.alibaba</groupId> <ar ...
- AngularJS(七):表单-单选框、下拉列表
本文也同步发表在我的公众号“我的天空” 单选框 在AngulerJS中单选框的处理与复选框有不少相似之处,我们来看以下示例: <body ng-app="myApp" ng- ...
- Hello World另类写法
#include<iostream> #define _ using namespace std; #define __ int main() #define ___ { #define ...
- shell命令cut
cut命令用来操作字符串,可以理解为剪切字符串的工具: cut有两种用法: 1.剪切字符串中的单个字符(-c参数) 例如: str=abcdef echo $str | cut -c 1-1 输出:a ...
- HDU 5489 Removed Interval 2015 ACM/ICPC Asia Regional Hefei Online (LIS变形)
定义f[i]表示以i为开头往后的最长上升子序列,d[i]表示以i为结尾的最长上升子序列. 先nlogn算出f[i], 从i-L开始枚举f[i],表示假设i在最终的LIS中,往[0,i-L)里找到满足a ...
- 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest J Computer Network1 (缩点+最远点对)
题意:在连通图中,求一条边使得加入这条边以后的消除的桥尽量多. 在同一个边双连通分量内加边肯定不会消除桥的, 求边双连通分量以后缩点,把桥当成边,实际上是要选一条最长的链. 缩点以后会形成一颗树,一定 ...
- ansible 任务委派 delegate_to
ansible 任务委派功能delegate_to run_noce: true 在一个主机上面只执行一次一个任务. ,如果没有这个参数的话,每个playbook中的组的主机都会执行一次. 我们有的 ...
- 任务管理器 用 Ctrl + Shift + Esc 替换 Ctrl + Alt + Del
任务管理器 用 Ctrl + Shift + Esc 替换 Ctrl + Alt + Del
- python_98_面向对象_学校
class School(object):#以后都加object(基类) def __init__(self, name, addr): self.name = name self.addr = ad ...
- VMware vSphere6.0 服务器虚拟化部署安装图解
一 VMware vSphere部署的前期规划要点 1 vSphere的优点 (略) 2 如何利用现在的设备架构虚拟化环境 在虚拟化过程中,用户大多会考虑目前现有的服务器.存储.交换机等基础设备是否可 ...