CodeForces 780CAndryusha and Colored Balloons (DFS)
来源:互联网 发布:知聊赚钱是真的吗 编辑:程序博客网 时间:2024/06/06 07:36
you can find the problem here :http://codeforces.com/problemset/problem/780/C
You have a tree with N nodes and you are asked to find a dye solution for each node that child nodes of this node have different color and this node ‘s color is also different from it’s child node.
so we can simplify the question : if a node is not dyed , it’s color can’t be same it’s parent and it’s grandparent .This conclusion can be easily prove by draw some tree……
#include <bits/stdc++.h>using namespace std;int a[200005],vis[200005];int n;vector<int> edge[200005];void dfs(int u,int prevc,int cur,int p){ a[u] = cur; int k = 1; for(auto v : edge[u]) { if(v != p) { while(k == cur || k == prevc) k++; dfs(v,cur,k,u); k++; } } return;}int main(){ cin>>n; int mx = 1; for(int i = 0;i<n-1;i++) { int u,v; cin>>u>>v; edge[u].push_back(v); edge[v].push_back(u); mx = max(mx,max((int)edge[u].size(),(int)edge[v].size())); } cout<<mx+1<<endl; dfs(1,-1,1,-1); for(int i = 1;i<=n;i++) { cout<<a[i]<<" "; } return 0;}
阅读全文
0 0
- CodeForces 780CAndryusha and Colored Balloons (DFS)
- Codeforces 780C Andryusha and Colored Balloons 搜索dfs
- CodeForces 780C Andryusha and Colored Balloons【DFS】
- codeforces 780-C. Andryusha and Colored Balloons(dfs)
- Codeforces 780C-Andryusha and Colored Balloons dfs
- Codeforces 780C-Andryusha and Colored Balloons
- Codeforces 782C Andryusha and Colored Balloons dfs
- Codeforces Round #403 C. Andryusha and Colored Balloons(DFS)
- codeforces 781A Andryusha and Colored Balloons(树上dfs)
- 【Codeforces 781 A Andryusha and Colored Balloons】+ DFS
- Codeforces Andryusha and Colored Balloons
- 【codeforces 782C】Andryusha and Colored Balloons
- codeforces 782c Andryusha and Colored Balloons
- Codeforces 782C Andryusha and Colored Balloons【思维+Dfs暴力染色】
- Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons dfs(树)
- Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons(DFS 邻接表 数据结构)
- Codeforces Round #403 (Div. 2)C Andryusha and Colored Balloons (dfs)
- Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons DFS
- Quick的基本结构及如何开始入门
- 核电站问题
- win10+anaconda2+cuda8.0+cudnn6.0安装tensorflow-gpu
- 流程控制
- 源代码安装paddlepaddle 到云计算机——感谢百度云和人工智能项目paddlepaddle
- CodeForces 780CAndryusha and Colored Balloons (DFS)
- 深入分析Volatile的实现原理
- 面向对象编程的概念
- OpenStackMitaka版双节点搭建架构图
- Porto竞赛第一名解决方法
- Android使用百度地图定位程序运行失败
- CentOS 7 安装 Docker
- java第二篇 枚举类的使用
- JAVA | 56