题目大意:

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

基本思路:

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. 求超大文件上传方案( Java )

    最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现. 在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表 ...

  2. Integrating .NET Code and SQL Server Reporting Services

    SQL Server Reporting Services versions 2000 and 2005 (SSRS) has many powerful features. SSRS has a w ...

  3. “The creator of this fault did not specify a Reason” Exception

    “The creator of this fault did not specify a Reason” Exception if(!DidItPass) { InvalidRoutingCodeFa ...

  4. I am going to India on a business trip

    I think English is very important,and learn it must perseverant.So I determined to write diary on Bo ...

  5. MATLAB 用 imresize() 函数缩小图象是 double 和 uint8 有差别

    今天发现一个奇怪的现象. 在用 imresize() 缩小图象时,如果图象时 double 格式的,缩小后会产生不连通的现象. 下面是原图: 对这张图象 img 做 simg = imresize(i ...

  6. Visual Studio Code-使用Chrome Debugging for VS Code调试JS

    准备工作 安装 Debugger for Chrome 插件 按 F5(或选择菜单栏的 Debug->Start Debuging),然后选择 Chrome,就会自动创建默认的配置文件 &quo ...

  7. 《图解设计模式》读书笔记3-3 Builder模式

    目录 示例程序 类图 代码 角色 思路拓展 谁知道什么 构造和实现分离 和Template Method模式的区别和联系? Builder模式即建造者模式,利用这个模式可以组装具有复杂结构的实例. 示 ...

  8. MySQL 导入导出 CSV 文件

    导入 导出 清空表中的所有数据 注意事项 常见问题 ERROR 1290 (HY000): The MySQL server is running with the --secure-file-pri ...

  9. flex布局相关用法

    /* pages/classic/classic.wxss */ .chunk { /* 行内元素可设置但是设置了flex,无效了 *//* display: inline-block; */ wid ...

  10. spring boot 控制 序列化格式

    @PostMapping(path = "/register", produces={MediaType.APPLICATION_JSON_VALUE, MediaType.TEX ...