Codeforces 963B Destruction of a Tree 思维+dfs
题目大意:
给出一棵树,每次只能摧毁有偶数个度的节点,摧毁该节点后所有该节点连着的边都摧毁,判断一棵树能否被摧毁,若能,按顺序输出摧毁的点,如果有多种顺序,输出一种即可
基本思路:
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的更多相关文章
- 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 ...
- 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 ...
- codeforces 682C Alyona and the Tree(DFS)
题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...
- codeforces 1076E Vasya and a Tree 【dfs+树状数组】
题目:戳这里 题意:给定有n个点的一棵树,顶点1为根.m次操作,每次都把以v为根,深度dep以内的子树中所有的顶点(包括v本身)加x.求出最后每个点的值为多少. 解题思路:考虑到每次都只对点及其子树操 ...
- codeforces 812E Sagheer and Apple Tree(思维、nim博弈)
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...
- 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 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...
- codeforces 220 C. Game on Tree
题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...
- Codeforces963B - Destruction of a Tree
Portal Description 给出一个\(n(n\leq2\times10^5)\)个点的树,每次可以删除一个度数为偶数的点及其相连的边,求一种能够删掉整棵树的方案. Solution 简单起 ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- linux如何查看端口被哪个进程占用的方法
linux如何查看端口被哪个进程占用的方法: 1.lsof -i:端口号2.netstat -tunlp|grep 端口号 都可以查看指定端口被哪个进程占用的情况[步骤一]lsof -ilsof -i ...
- MatrixTraceTransform主要逻辑在transform方法中
@Override public void transform(TransformInvocation transformInvocation) throws TransformException, ...
- DHCP原理
一台主机的ip地址可用通过两种方式来设置.1 手动输入:2 自动向DHCP服务器获取.手动输入会出现错误,比如输入一个已经分配的ip地址,当内网机器只有几台,十几台还可以忍受,如果是几百台呢,不可能一 ...
- openstack介绍及共享组件——消息队列rabbitmq
一.云计算的前世今生 所有的新事物都不是突然冒出来的,都有前世和今生.云计算也是IT技术不断发展的产物. 要理解云计算,需要对IT系统架构的发展过程有所认识. 请看下 IT系统架构的发展到目前为止大致 ...
- 使用pgAdmin3将postgreSQL中的数据导出insert格式的sql文件
第一步: 第二步: 第三步: 第四步: 成功:
- vue双花括号的使用
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- Altium Designer chapter1总结
第一章操作基础中有以下几点需要注意: (1)随着DSP.ARM.FPGA等高速逻辑元件的应用,PCB的信号完整性与抗干扰性能显得尤为重要. (2)Altium Designer的发展史:Protel ...
- SQLServer2008查询时对象名无效
情况一:如果表名是关键字,查询时把表名括起来,不作为关键字使用 情况二:看左上角显示的是否是master,这是数据库的默认系统库,点选这个改成自己的即可
- redis可以做什么?
redis可以做什么? 1.缓存,毫无疑问这是Redis当今最为人熟知的使用场景.在提升服务器性能方面非常有效: 2.排行榜,如果使用传统的关系型数据库来做这个事儿,非常的麻烦,而利用Redis的So ...
- 《JAVA设计模式》之外观模式(Facade)
在阎宏博士的<JAVA与模式>一书中开头是这样描述门面(Facade)模式的: 门面模式是对象的结构模式,外部与一个子系统的通信必须通过一个统一的门面对象进行.门面模式提供一个高层次的接口 ...