963B:Destruction of a Tree
You are given a tree (a graph with n vertices and n - 1 edges in which it's possible to reach any vertex from any other vertex using only its edges).
A vertex can be destroyed if this vertex has even degree. If you destroy a vertex, all edges connected to it are also deleted.
Destroy all vertices in the given tree or determine that it is impossible.
The first line contains integer n (1 ≤ n ≤ 2·105) — number of vertices in a tree.
The second line contains n integers p1, p2, ..., pn (0 ≤ pi ≤ n). If pi ≠ 0 there is an edge between vertices i and pi. It is guaranteed that the given graph is a tree.
If it's possible to destroy all vertices, print "YES" (without quotes), otherwise print "NO" (without quotes).
If it's possible to destroy all vertices, in the next n lines print the indices of the vertices in order you destroy them. If there are multiple correct answers, print any.
50 1 2 1 2
YES12354
40 1 2 3
NO
In the first example at first you have to remove the vertex with index 1 (after that, the edges (1, 2) and (1, 4) are removed), then the vertex with index 2 (and edges (2, 3) and (2, 5) are removed). After that there are no edges in the tree, so you can remove remaining vertices in any order.
规定一个顺序,使分为父子结点,则每次删除只能往子节点查找
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <stack> #include <cstdlib> #include <iomanip> #include <cmath> #include <cassert> #include <ctime> #include <map> #include <set> using namespace std; #pragma comment(linker, "/stck:1024000000,1024000000") #define lowbit(x) (x&(-x)) #define max(x,y) (x>=y?x:y) #define min(x,y) (x<=y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.1415926535897932384626433832 #define ios() ios::sync_with_stdio(true) #define INF 0x3f3f3f3f #define mem(a) (memset(a,0,sizeof(a))) #define ll long long vector<int>v[200005],ans; stack<int>q; int cnt[200005],vis[200005]; int n,x,pos,par[200005]; void bfs(int now) {ans.push_back(now);vis[now]=1;for(int i=0;i<v[now].size();i++){cnt[v[now][i]]--;if(v[now][i]==par[now] || vis[v[now][i]]) continue;//当前节点已经被找过,或者是now节点的父节点if(!(cnt[v[now][i]]&1)) bfs(v[now][i]);} } void dfs(int fa,int now) {par[now]=fa;q.push(now);for(int i=0;i<v[now].size();i++){if(v[now][i]==fa) continue;dfs(now,v[now][i]);} } int main() {scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&x);if(x){v[i].push_back(x);v[x].push_back(i);cnt[i]++;cnt[x]++;}else pos=i;}dfs(0,pos);//n-1条边,则必有一个为0,不妨把这点作为根节点遍历。memset(vis,0,sizeof(vis));while(!q.empty()){int fr=q.top();q.pop();if(!(cnt[fr]&1)) bfs(fr);//从后向前遍历,若存在,必只有一个结点符合初始为偶数 }if(ans.size()==n){printf("YES\n");for(int i=0;i<ans.size();i++)printf("%d\n",ans[i]);}else printf("NO\n");return 0; }
转载于:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/8883723.html
963B:Destruction of a Tree相关推荐
- 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 ...
- iview 下拉select样式_Vue.js相关:iview实现select tree树形下拉框的示例代码
Vue.js相关:iview实现select tree树形下拉框的示例代码 发布于 2020-3-16| 复制链接 摘记: 本文介绍了iview实现select tree树形下拉框的示例代码,分享给大 ...
- 论文笔记:Straight to the Tree: Constituency Parsing with Neural Syntactic Distance
论文笔记:Straight to the Tree: Constituency Parsing with Neural Syntactic Distance 目录 论文笔记:Straight to t ...
- 论文精读:XGBoost: A Scalable Tree Boosting System
论文下载地址:XGBoost: A Scalable Tree Boosting System 一句话讲: 读前先问 读论文之前首先要问几个问题: 这篇论文大方向的目标是什么? 机器学习中的有监督学习 ...
- CMU 15-445实验记录(三):Project 2 B+Tree的插入与删除
CMU 15-445实验记录(三):Project 2 B+Tree的插入与删除 B+Tree的删除的五种情况: 叶结点被删除后没有underflow,直接删除对应的key和recordPtr即可 叶 ...
- Codeforces 963B Destruction of a Tree 【贪心】
本题的贪心策略是:每次删除连到叶子结点的dfs链上离根最远的偶数度的结点 greed is good 实现方法是先维护一个degree[i]表示第i个点有多少个度,然后dfs,当每一个结点的所有子节点 ...
- VS2010/MFC编程入门之三十(常用控件:树形控件Tree Control 上)
前面两节为大家讲了列表视图控件List Control,这一节开始介绍一种特殊的列表--树形控件Tree Control. 树形控件简介 树形控件在Windows系统中是很常见的,例如资源管理器左侧的 ...
- 数据结构学习笔记(六):二叉树(Binary Tree)
目录 1 背景知识:树(Tree) 2 何为二叉树(Binray Tree) 2.1 二叉树的概念与结构 2.2 满二叉树与完全二叉树 2.3 二叉树的三种遍历方式 3 二叉树及其遍历的简单实现(Ja ...
- 转帖:BTree,B-Tree,B+Tree,B*Tree都是什么
BTree,B-Tree,B+Tree,B*Tree都是什么 转帖自:http://blog.csdn.net/manesking/archive/2007/02/09/1505979.aspx &l ...
- VS2019/MFC编程入门:树形控件Tree Control 下
前面一节讲了树形控件Tree Control的简介.通知消息以及相关数据结构,本节继续讲下半部分,包括树形控件的创建.CTreeCtrl类的主要成员函数和应用实例.在内容开始前为大家介绍一款MFC界面 ...
最新文章
- 人脸识别被玩坏?别急,“护脸计划”即将开启
- 从键盘输入一串连续的数字,判断输出是否为电话号码
- 1.3 函数调用反汇编解析以及调用惯例案例分析
- 【技术史】数据中台的前世今生
- JSP URL重写-urlrewrite
- 语法和c区别_dockerfile语法
- 中国碳纤维复合加热元件行业市场供需与战略研究报告
- 撸一款Flutter版『微信』
- 天锋w2019_三星看了想打人,华强北神机天锋W2019现世,专卖店都难辨真假
- IT从业者创业公司生存指南:创业中期 ---- 先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。
- 手机可以连上wifi,电脑连不上怎么办?
- DataGridView绑定null后再次绑定DataSource列标题成英文
- java 获取明天12点日期
- 大巧不工Web前端设计修炼之道——(8)浅谈Web发展的未来
- MSSQL Server 2008 - express 版 安装 企业管理器Management Studio
- 2022年9月青少年C/C++软件编程(四级)等级考试试卷及答案解析
- 面向对象的讨论-2022年5月4日
- 八、血条的制作和boss敌人的产生(雷霆战机)
- WORD之smartart
- 关于高频信号PCB挖空