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. java中不常见的关键字:strictfp

    1.strictfp, 即 strict float point (精确浮点). strictfp 关键字可应用于类.接口或方法.使用 strictfp 关键字声明一个方法时,该方法中所有的float ...

  2. Echarts API说明文档

    theme = {        // 全图默认背景        // backgroundColor: 'rgba(0,0,0,0)',                // 默认色板        ...

  3. json字符串,json对象,java对象互相转换

    1.把JSON字符串转换为JAVA 对象 JSONObject jsonobject = JSONObject.fromObject(jsonStr); User user= (User)JSONOb ...

  4. SAS学习笔记38 SAS Comments注释语句

    通常来讲,注释语句有四种: 1.* message; 2.COMMENT message; 3./* message */ 4.%* message; 第一种的主要限制是注释之中不得有“:”符号.通常 ...

  5. JSP JSONArray使用遇坑!添加以下6个jar包

    1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons ...

  6. Deep Learning方向的paper

    转载 http://hi.baidu.com/chb_seaok/item/6307c0d0363170e73cc2cb65 个人阅读的Deep Learning方向的paper整理,分了几部分吧,但 ...

  7. nginx 反向代理的配置

    nginx中的每个server就是一个反向代理配置,可以有多个server(nginx只能处理静态资源) nginx中 server的配置 server { listen 80; server_nam ...

  8. Ubuntu的apt-get代理设置

    三种方法 -o选项 # sudo apt-get -o Acquire::http::proxy="http://127.0.0.1:8080/" update 配置文件 # vi ...

  9. SAP UI5的support Assistant

    SAP UI5的support Assistant给UI5刚入门的开发人员提供了一种极便利的快速熟悉UI5代码的途径. 召唤方式: ctrl+shift+alt+p四个键同时按,在弹出的对话框里点击按 ...

  10. BufferInputStream、BufferOutputStream、BufferedReader、BufferedWriter、Java代码使用BufferedReader和BufferedWriter实现文本文件的拷贝

    BufferInputStream和BufferOutputStream的特点: 缓冲字节输入输出流,缓冲流是处理流,它不直接连接数据源/目的地,而是以一个字节流为参数,在节点流的基础上提供一些简单操 ...