[构造] Codeforces 717E Bubble Cup 9 - Finals E. Paint it really, really dark gray
来源:互联网 发布:淘宝免费申请试用在哪 编辑:程序博客网 时间:2024/05/17 01:02
这是一道递归构造的题
先构造完子树,然后可以通过走father来调整自己
因为1没有father 需要特判 走一个儿子来调整
#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; if (p1==p2){ p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; } return *p1++;}inline void read(int &x){ char c=nc(),b=1; for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1; for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;}const int N=200005;struct edge{ int u,v,next;}G[N<<1];int head[N],inum;inline void add(int u,int v,int p){ G[p].u=u; G[p].v=v; G[p].next=head[u]; head[u]=p;}int n;int tag[N],clr[N];#define V G[p].vinline void dfs(int u,int fa){ tag[u]=clr[u]; for (int p=head[u];p;p=G[p].next) if (V!=fa) dfs(V,u),tag[u]|=tag[V];}inline void solve(int u,int fa){ if (!tag[u]) return; printf("%d ",u); clr[u]^=1; for (int p=head[u];p;p=G[p].next) if (V!=fa) solve(V,u); if (fa){ if (clr[u]) printf("%d %d %d ",fa,u,fa),clr[u]^=1; else printf("%d ",fa),clr[fa]^=1; }}int main(){ int iu,iv; freopen("t.in","r",stdin); freopen("t.out","w",stdout); read(n); for (int i=1;i<=n;i++) read(clr[i]),clr[i]=(clr[i]==-1)?1:0; for (int i=1;i<n;i++){ read(iu); read(iv); add(iu,iv,++inum); add(iv,iu,++inum); } if (clr[1]==1){ int flag=0; for (int i=2;i<=n;i++) flag|=clr[i]; if (!flag) return printf("%d %d %d %d ",1,G[head[1]].v,1,G[head[1]].v),0; } clr[1]^=1; dfs(1,0); solve(1,0); if (clr[1]){ iv=G[head[1]].v; printf("%d %d %d ",iv,1,iv); } return 0;}
0 0
- [构造] Codeforces 717E Bubble Cup 9 - Finals E. Paint it really, really dark gray
- codeforces 717E - Paint it really, really dark gray
- 【DFS】Codeforces 717E Paint it really, really dark gray
- 【DFS】Codeforces717E[Paint it really, really dark gray]题解
- [dfs] Codeforces717E. Paint it really, really dark gray
- Bubble Cup 9 - Finals E. 搜索
- [随机化 构造] Codeforces 717H Bubble Cup 9 - Finals H. Pokermon League challenge
- 图论+DFS——Bubble Cup 9 - Finals [Online Mirror] E
- 【Codeforces】【Coder-Strike 2014 - Finals】【E Cup Trick】【题解】
- VK Cup 2012 Finals E
- [排序不等式] Codeforces 717C Bubble Cup 9 - Finals C. Potions Homework
- [矩阵快速幂加速DP] Codeforces 717D Bubble Cup 9 - Finals D. Dexterina’s Lab
- [矩阵快速幂加速DP]Codeforces 717D Bubble Cup 9 -Finals D. Dexterina’s Lab
- 【Codeforces 817 C. Really Big Numbers】+ 构造
- Bubble Cup 9 - Finals [Online Mirror] 题解
- [无源汇最大费用可行流 差分费用流] Codeforces 717G Bubble Cup 9 - Finals G. Underfail
- Codeforces Bubble Cup 8 - Finals [Online Mirror] 解题报告
- Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) E. Rusty String(fft)
- HDU 5875 Function (思路题目)
- [矩阵快速幂加速DP] Codeforces 717D Bubble Cup 9 - Finals D. Dexterina’s Lab
- C++引用的基本概念与应用
- [随机化 构造] Codeforces 717H Bubble Cup 9 - Finals H. Pokermon League challenge
- 数据库系统概论之第二章关系数据库
- [构造] Codeforces 717E Bubble Cup 9 - Finals E. Paint it really, really dark gray
- 51nod 1212 无向图最小生成树
- 2016.09.10【初中部 NOIP普及组 】模拟赛题解
- 51nod 1714 B君的游戏
- SQL子查询的一些例子
- 1-XHTML导论
- 常用的几个UI美化
- 51Nod-1390-游戏得分
- jQuery使用ajaxSubmit()提交表单示例