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 ...
随机推荐
- BZOJ 4836: [Lydsy1704月赛]二元运算 分治FFT
Code: #include<bits/stdc++.h> #define ll long long #define maxn 500000 #define setIO(s) freope ...
- Linux下安装Harbor 1.8.0 仓库的安装和使用(亲测)
根据Harbor官方描述: Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如安全.标识和管理等,扩展了开源Docker Distri ...
- 【HDOJ6635】Nonsense Time(时间倒流,lis)
题意:给定n个数的数列,第i个数为a[i],刚开始所有位置都处于禁用状态,第i次之后位置p[i]变为可用,求每次变化后的lis长度 n,a[i],p[i]<=5e4 保证a[i],p[i]均为随 ...
- RxJava学习总结
1. 概念 Rx是微软.NET的一个响应式扩展.Rx借助可观测的序列提供一种简单的方式来创建异步的,基于事件驱动的程序.Rx就是一种响应式编程,来创建基于事件的异步程序RxJava是一个在 Java ...
- UVA 111 历史考试
题目描述:最长公共子序列的变形 题目序列中第i项是学生给第i号历史事件排出的序号,另外还给出了第i号历史事件的正确序号 求按照学生给出的序号排好历史事件后,所得的事件排序与历史事件实际发生的序列的最长 ...
- Hive 窗口函数之 lead() over(partition by ) 和 lag() over(partition by )
lead函数用于提取当前行前某行的数据 lag函数用于提取当前行后某行的数据 语法如下: lead(expression,offset,default) over(partition by ... o ...
- Python中import的使用方法
源文出处: "import"的本质参照: Python中import机制 python导入自定义模块和包
- Gradient Descent with Momentum and Nesterov Momentum
在Batch Gradient Descent及Mini-batch Gradient Descent, Stochastic Gradient Descent(SGD)算法中,每一步优化相对于之前的 ...
- debian7下安装eclipse
apt-get install build-essential 完成后从eclipse官网上下载C++专用的版本,直接解压缩即可
- Spring Security 02
权限管理 配置不过滤的资源 方法1 <http pattern="/login.jsp" security="none"></http> ...