题目大意:

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

基本思路:

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. 退役——halfrot's life in OI

    这是一个没有人看的博客里丢了两年的坑,还有很多事应该做,但是我很懒,所以今天把它填了. 前记:和很多人的竞赛生涯一样,一开始我也是奋不顾身,奔月而去,然而身处弱校,没有人引导方向,再加上自己很蒻的主要 ...

  2. Harbor修改暴露端口

    把原来的端口映射改成1180 一 修改docker-compose.yml [root@topcheer ~]# cat /mnt/harbor/docker-compose.yml version: ...

  3. ward's method分层聚类凝聚法

    ward's method是分层聚类凝聚法的一种常见的度量cluster之间距离的方法,其基本过程是这样的(参考:http://blog.sciencenet.cn/blog-2827057-9217 ...

  4. UNITY编辑器模式下static变量的坑

    在unity中写编辑器扩展工具,如在编辑器中加个菜单,点击这个菜单项时执行打包功能. 类如下,其中的静态变量,如果每次进来不清空,则LIST会越来越大,打包函数执行完后系统不会帮我们清空 #if UN ...

  5. windowns环境下mysql 安装教程

    windowns环境下mysql 安装教程 一:这里以绿色版安装为例(解压就可以使用) 下载地址: 下载页面:https://dev.mysql.com/downloads/mysql/  2:点击 ...

  6. JPEG 存储分割后的图象会产生锯齿

    分割后的图象如果保存为 JPG 格式,物体边界会产生锯齿状的很暗的像素. 如下图所示. 这会给后续处理带了噪声, 因此,保存分割后的图象最好用无损压缩格式,如 bmp, png 等.

  7. 凉经-乐糖游戏-PHP开发实习生

    收到面试通知当天因为学校出事要求我明天必须回去,所以就买当晚的火车票,然后跟公司说学校有事明天没法去面试了,公司人事比较好给我安排到当天下午面试.公司规模不是很大,但位置好下了地铁到,可能因为招的是实 ...

  8. grpc应用于微服务的分析,基于python

    grpc应用于微服务的分析 gRPC 是一个高性能.开源和通用的 RPC 框架,面向移动和 HTTP/2 设计,目前提供 C.Java 和 Go 语言版本,分别是:grpc, grpc-java, g ...

  9. python实现操作mysql数据库

    实现代码如下: #mysql数据库的查询等 import pymysql from xctest_tools.xc_ReadFile.get_ReadTxt import * class mysql: ...

  10. Mac004--Tomcat安装

    Mac--Tomcat安装 一.Tomcat下载 https://tomcat.apache.org/download-80.cgi 下载Tomcat注意tomcat与jdk版本要一致.jdk1.8版 ...