OvO http://codeforces.com/contest/963/problem/B

  CF 963B 964D

  对于题目要求,显然一开始的树,要求度数为偶数的节点个数为奇数个,通过奇偶讨论显然这个树总节点个数为奇数个。

  然后对于每一步分割出来的树林,显然树林中每个树都得满足度数为偶数的节点个数为奇数个,同样地,可以得到树林中每个树的总结点为奇数个

  其实上面写的一些是废话,后文并不会用到……

  首先排除度数为偶数的节点有偶数个的树,即已知要讨论的树均满足度数为偶数的节点有奇数个

  从叶子节点往上搜(采用回溯),搜到第一个度数为偶数的节点 X,那么以 X 为根的子树中,必然只有这个根节点 X 的度数为偶数。

  那么由于整棵树度数为偶数的节点有奇数个,所以去掉以 X 为根的子树后(X的父节点度数会发生改变),剩下的节点中,度数为偶数的节点仍然为奇数个。

  对于以 X 为根的子树,因为除了根节点 X 以外其他节点度数均为奇数,所以可以通过一个简单的 DFS 直接清除整棵以 X 为根节点的子树

  去掉 X 后继续往上回溯,找到下一个度数为偶数的节点,过程同上。

#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
#include <set> using namespace std; const int M=2e5+44; vector<int> vec[M];
int du[M],tag[M];
int n;
queue<int> que;
int ans[M],lans; void dfs(int rt,int fa)
{
int v,now,tmp;
for(int i=0;i<vec[rt].size();i++)
if(vec[rt][i]!=fa)
dfs(vec[rt][i],rt);
if((du[rt]&1)==0)
{
que.push(rt); du[fa]--;
while(!que.empty())
{
now=que.front(); que.pop();
ans[++lans]=now; tag[now]=1;
for(int i=0;i<vec[now].size();i++)
{
v=vec[now][i]; if(v==fa) continue; if(tag[v]) continue;
du[v]--; que.push(v);
}
}
}
} int main()
{
int tmp;
memset(du,0,sizeof(du));
scanf("%d",&n);
if((n&1)==0)
return puts("NO"),0;
for(int i=1;i<=n;i++)
vec[i].clear();
for(int i=1;i<=n;i++)
{
scanf("%d",&tmp); if(tmp==0) continue;
du[i]++; du[tmp]++;
vec[i].push_back(tmp); vec[tmp].push_back(i);
}
while(!que.empty()) que.pop();
dfs(1,-1);
if(lans!=n) return puts("??"),0;
puts("YES");
for(int i=1;i<=lans;i++)
printf("%d\n",ans[i]);
return 0;
}

  

Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1) 963B 964D B Destruction of a Tree的更多相关文章

  1. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)D. Frequency of String

    题意:有一个串s,n个串模式串t,问s的子串中长度最小的包含t k次的长度是多少 题解:把所有t建ac自动机,把s在ac自动机上匹配.保存每个模式串在s中出现的位置.这里由于t两两不同最多只有xsqr ...

  2. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2)

    A. Splits time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  3. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)

    A. Alternating Sum 就是个等比数列,特判公比为 $1$ 的情况即可. #include <bits/stdc++.h> using namespace std; ; ; ...

  4. CodeForces 837D - Round Subset | Educational Codeforces Round 26

    /* CodeForces 837D - Round Subset [ DP ] | Educational Codeforces Round 26 题意: 选k个数相乘让末尾0最多 分析: 第i个数 ...

  5. Codeforces 932 A.Palindromic Supersequence (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))

    占坑,明天写,想把D补出来一起写.2/20/2018 11:17:00 PM ----------------------------------------------------------我是分 ...

  6. ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) A

    2018-02-19 A. Palindromic Supersequence time limit per test 2 seconds memory limit per test 256 mega ...

  7. Divide by Zero 2018 and Codeforces Round #474 (Div. 1 + Div. 2, combined)

    思路:把边看成点,然后每条边只能从下面的边转移过来,我们将边按照u为第一关键字,w为第二关键字排序,这样就能用线段树维护啦. #include<bits/stdc++.h> #define ...

  8. ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined)

    靠这把上了蓝 A. Palindromic Supersequence time limit per test 2 seconds memory limit per test 256 megabyte ...

  9. Codeforces 932 C.Permutation Cycle-数学 (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))

    C. Permutation Cycle   time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

随机推荐

  1. Mysql主从复制原理及同步延迟问题

    本文转载自:Mysql主从复制原理及同步延迟问题 主从复制解决的问题 数据分布:通过复制将数据分布到不同地理位置 负载均衡:读写分离以及将读负载到多台从库 备份:可作为实时备份 高可用性:利用主主复制 ...

  2. FishingMaster(HDU-6709)【贪心】

    题目链接:https://vjudge.net/problem/HDU-6709 题意:一个人要抓n条鱼,每抓一条鱼用时K,每烹饪一条鱼用时a[i],抓鱼的过程不能被打断,烹饪鱼的时候可以抓鱼,也可以 ...

  3. pandas 索引笔记

    import pandas as pd import numpy as np s = pd.Series(np.random.rand(5), index=list('abcde')) # 创建序列, ...

  4. Python开发【第四章】:函数剖析

    一.Python函数剖析 1.函数的调用顺序 #!/usr/bin/env python # -*- coding:utf-8 -*- #-Author-Lian #函数错误的调用方式 def fun ...

  5. 一文搞懂嵌入式uboot、kernel、文件系统的关系

    总览: 在linux系统软件架构可以分为4个层次(从低到高分别为):   1.引导加载程序         引导加载程序(Bootloader)是固化在硬件Flash中的一段引导代码,用于完成硬件的一 ...

  6. css的导入与基础选择器

    css是什么 css也是一门标记语言,主要用作修改控制html的样式 css书写的位置(导入) css是用来控制页面标签的样式,但是可以根据实际情况书写在不同的位置, 放在不同位置有不同的专业叫法,可 ...

  7. springload热更新的优缺点

    java开发web应用没有.net的方便快捷, 原因是传统开发模式下新增修改代码后要查看效果, 一般要重启应用, 导致浪费了许多无谓的时间,没有.net的高效, 任意更新文件实时生效. 但是有个叫sp ...

  8. spring-security2配置精讲(转载)

    本文转载自牛人downpour的帖子: http://www.iteye.com/topic/319965 Spring 论坛上看了不少Spring Security的相关文章.这些文章基本上都还是基 ...

  9. 【Distributed】分布式系统中遇到的问题

    一.概述  大型互联网公司公司一般都采用服务器集群,这样就要实现多个服务器之间的通讯,在nginx实现负载均衡(分布式解决方案)服务器集群会产生那些问题? 分布式锁(基本)单纯的Lock锁或者syn ...

  10. c# String 常用方法应用