CF 717E[dfs]
来源:互联网 发布:如何注册自己的域名 编辑:程序博客网 时间:2024/05/29 07:34
题意:给定一棵树,每个节点初始有颜色,共两种颜色.你一开始在点1,走到哪个点就把它的颜色改变.输出一种合法的方案使最终的树颜色都变为
dfs构造
对每个节点如果当前为-1就从它到父亲节点走一遍
如果当前是root为-1没有父亲就随便找个儿子走一遍再走到那个儿子上(因为是dfs处理它的儿子都已经是1了)
#include<cstdio>#define N 200005using namespace std;inline char nc() { static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*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;}struct edge{int to,next;}e[N<<1];int n,cnt,head[N],clo[N],ans[N<<1];inline void add(int x,int y) { e[++cnt].next=head[x],e[cnt].to=y,head[x]=cnt; e[++cnt].next=head[y],e[cnt].to=x,head[y]=cnt;}#define ans_push(x) ans[++ans[0]]=xvoid dfs(int x,int fa) { clo[x]^=1; ans_push(x); for(int i=head[x];i;i=e[i].next) if(e[i].to!=fa) { dfs(e[i].to,x); ans_push(x); clo[x]^=1; } if(clo[x]==0) if(x==1) { ans_push(e[head[x]].to); ans_push(x); ans_push(e[head[x]].to); } else { ans_push(fa); ans_push(x); clo[fa]^=1; clo[x]^=1; }}int main() { read(n); int flag=0; for(int i=1;i<=n;++i) { read(clo[i]); if(clo[i]==-1) flag=1,clo[i]=0; } for(int i=1,x,y;i<n;++i) { read(x),read(y); add(x,y); } if(!flag) puts("1"); else { clo[1]^=1; dfs(1,0); for(int i=1;i<=ans[0];++i) printf("%d%s",ans[i],i==ans[0]?"\n":" "); }}
阅读全文
0 0
- CF 717E[dfs]
- CF 208E Blood Cousins(二分+DFS)
- CF 600E(Lomsat gelral-dfs)
- Graph Cutting CF 405E DFS
- CF#701 E. Connecting Universities (DFS)
- CF 258E Little Elephant and Tree(DFS+线段树)
- [CF]291E. Tree-String Problem | dfs+kmp
- CF 297 div2 E. Anya and Cubes (hash+dfs)
- 【CF】E. Anya and Cubes(双向DFS)
- CF Educational Codeforces Round 6 E题 dfs+线段树
- CF--- PROBLEM 620 E 【思维 + DFS序 + 二进制状态】
- cf-e
- CF-27E - Number With The Given Amount Of Divisors(枚举+dfs)
- CF-48E. Ivan the Fool VS Gorynych the Dragon(bfs+dfs+判重回路)
- CF 258E Little Elephant and Tree 【线段树,树上DFS序列】
- cf-edu#6-E - New Year Tree -dfs序+线段树维护
- 【CF 620E 】New Year Tree 【DFS+线段树+状态压缩】
- CF#877 E. Danil and a Part-time Job(DFS序+线段树)
- c# MVC中的ViewData、ViewBag
- 原码, 反码, 补码 详解
- python语法笔记
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- 巩固基础——操作系统(2)操作系统的运行环境与运行机制
- CF 717E[dfs]
- StringBuffer和StringBuilder的区别
- SQL基础
- 在hive中创建表,关联到hdfs文件路径,而不需导入数据到hive表
- sql语句:不为空
- hibernate实现分页的几种方式
- C#读写二进制文件
- 内核LCD驱动结构分析及实例分析
- python小结