题目大意:

给出一棵树,每次只能摧毁有偶数个度的节点,摧毁该节点后所有该节点连着的边都摧毁,判断一棵树能否被摧毁,若能,按顺序输出摧毁的点,如果有多种顺序,输出一种即可

基本思路:

1)我一开始自然而然想到的,当然是贪心,首先判断能否可行,然后我是想先从叶子到根摧毁一编,然后从根开始再摧毁,我觉得应该可行,还没试验;

2)rank前10的代码,我就不不自量力的去评价了,自己体会吧。

代码如下:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<vector> using namespace std; typedef long long ll;
typedef long long LL;
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f;
const int maxn = 200000+10;
const ll mod = 1e9+9; vector<int>gra[maxn];
bool ispos[maxn];
bool dfs(int u,int pre){
bool ans=1;
int sz=gra[u].size();
for(int i=0;i<sz;i++){
int v=gra[u][i];
if(v==pre) continue;
ispos[v]=dfs(v,u);
if(ispos[v]) ans^=1;
}
return ans;
}
void print(int u,int pre){
int sz=gra[u].size();
for(int i=0;i<sz;i++){
int v=gra[u][i];
if(v==pre) continue;
if(!ispos[v]) print(v,u);
}
printf("%d\n",u);
for(int i=0;i<sz;i++){
int v=gra[u][i];
if(v==pre) continue;
if(ispos[v]) print(v,u);
}
}
int main(){
int n,rt;
scanf("%d",&n);
for(int i=1;i<=n;i++){
int u;
scanf("%d",&u);
if(!u){
rt=i;
}else{
gra[u].push_back(i);
gra[i].push_back(u);
}
}
bool flag=dfs(rt,0);
if(flag){
printf("YES\n");
print(rt,0);
}else{
printf("NO\n");
}
return 0;
}

  

Codeforces 963B Destruction of a Tree 思维+dfs的更多相关文章

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

  3. codeforces 682C Alyona and the Tree(DFS)

    题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...

  4. codeforces 1076E Vasya and a Tree 【dfs+树状数组】

    题目:戳这里 题意:给定有n个点的一棵树,顶点1为根.m次操作,每次都把以v为根,深度dep以内的子树中所有的顶点(包括v本身)加x.求出最后每个点的值为多少. 解题思路:考虑到每次都只对点及其子树操 ...

  5. codeforces 812E Sagheer and Apple Tree(思维、nim博弈)

    codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...

  6. codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)

    codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  7. codeforces 220 C. Game on Tree

    题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...

  8. Codeforces963B - Destruction of a Tree

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

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

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

随机推荐

  1. SQL Server数据库的软硬件性能瓶颈

    在过去十年里,很多复杂的企业应用都是用Microsoft SQL Server进行开发和部署的.如今,SQL Server已经成为现代业务应用的基石,并且它还是很多大公司业务流程的核心.SQL Ser ...

  2. [CF959D]Mahmoud and Ehab and another array construction task题解

    解法 非常暴力的模拟. 一开始吧\(1 -> 2 \times 10^6\)全部扔进一个set里,如果之前取得数都是与原数组相同的,那么lower_bound一下找到set中大于等于它的数,否则 ...

  3. R语言预测实战(第一章)

    本例使用forecast包中自带的数据集wineind,它表示从1980年1月到1994年8月, 由葡萄酒生产商销售的容量不到1升的澳大利亚酒的总量.数据示意如下: #观察曲线簇 len=1993-1 ...

  4. Linux下安装Tomcat(2)

    Tomcat是一个免费的开源的Serlvet容器,它是Apache基金会的Jakarta项目中的一个核心项目,由Apache,Sun和 其它一些公司及个人共同开发而成.由于有了Sun的参与和支持,最新 ...

  5. git 几个commit点合并成一个commit点

    在用git做版本控制器的时候,经常会遇到以下情况: 1.在做1个功能的时候,你自己觉得代码没问题了,就本地commit,然后提交代码,在gitlab上发起和并请求,老大看完之后,觉得你还有修改的地方, ...

  6. Cookie和Session的区别和联系

    会话技术 1.Cookie 客户端会话技术 数据存储在客户端,只能存String类型,并且大小有限制,一般为4KB,Cookie数量有限制(20个),不同浏览器不同: 一个Tomcat服务器中的共享问 ...

  7. Codeforces 86D Powerful array (莫队)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  8. QT_study

    https://blog.csdn.net/a313827758/article/details/72736552 https://blog.csdn.net/xbcreal/article/deta ...

  9. Vagrant 入门 - 启动 vagrant 及 通过 ssh 登录虚拟机

    原文地址 在终端运行 vagrant up 命令即可启动 Vagrant 环境: $ vagrant up 不到一分钟,命令就会执行完毕,运行 Ubuntu 的虚拟机会启动成功.Vagrant 运行虚 ...

  10. Using Tensorflow SavedModel Format to Save and Do Predictions

    We are now trying to deploy our Deep Learning model onto Google Cloud. It is required to use Google ...