Codeforces Round #403 C. Andryusha and Colored Balloons
来源:互联网 发布:苹果电脑网络不可用 编辑:程序博客网 时间:2024/06/05 10:08
题目链接:点击打开链接
题意:给出n个点,(n-1)条无向边,问能否将点染色使所有点与其邻接的点以及距离为1的点的颜色不同(如:a与b存在一边,b与c存在一边,则a,b,c三点颜色应不同)
题解:将图看做一棵树,对于一个节点的子节点我们先将选一个节点进行染色,染色的颜色选择则是从1开始不断++一直到与其父节点以及父节点的父节点颜色不同为止,然后再对这个子节点进行进一步深搜,因为这个子节点的子节点不受这个子节点的兄弟的颜色影响所以可以继续深搜,搜完第一个子节点后则对第二个子节点进行染色,第二个子节点的颜色则是先++然后一直++到和其父节点以及父节点的父节点不同为止,然后再做与第一个子节点相同的深搜,以此类推即可。
#include<iostream>#include<stdio.h>#include<algorithm>#include<cmath>#include<stdlib.h>#include <string.h>#include<queue>#include<set>#include<map>#include<stack>#include<time.h>using namespace std;#define MAX_N 200005#define inf 0x3f3f3f3f#define LL long long#define ull unsigned long longconst LL INF = 1e18;const int mod = 1e8+7;typedef pair<double, int>P;int n;int mx;vector<int>G[MAX_N];int color[MAX_N];void dfs(int u, int fa){ int c = 0; for(int i=0; i<G[u].size(); i++) { if(G[u][i] != fa) { c++; while(c == color[u] || c == color[fa]) c++; color[G[u][i]] = c; mx = max(mx, c); } } for(int i=0; i<G[u].size(); i++) { if(G[u][i] != fa) { dfs(G[u][i], u); } }}int main(){ memset(color, 0, sizeof(color)); mx = 0; cin >> n; for(int i=1; i<n; i++) { int a, b; scanf("%d%d", &a, &b); G[a].push_back(b); G[b].push_back(a); } color[1] = 1; dfs(1, 1); printf("%d\n", mx); for(int i=1; i<=n; i++) printf("%d ",color[i]);}
0 0
- Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons
- Codeforces Round #403 C. Andryusha and Colored Balloons(DFS)
- Codeforces Round #403 div2 C. Andryusha and Colored Balloons
- Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons
- Codeforces Round #403 C. Andryusha and Colored Balloons
- 【Codeforces Round #403】Codeforces 781A Andryusha and Colored Balloons
- 【codeforces 782C】Andryusha and Colored Balloons
- codeforces 782c Andryusha and Colored Balloons
- Codeforces 780C-Andryusha and Colored Balloons
- 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
- Codeforces Andryusha and Colored Balloons
- C. Andryusha and Colored Balloons
- C. Andryusha and Colored Balloons
- C. Andryusha and Colored Balloons
- Codeforces 782C Andryusha and Colored Balloons dfs
- Linux下进程间通信方式——管道
- PAT-A1091
- POJ3617——Best Cow Line
- 要明确几个地方是结束不能占用的,
- 实例—ViewPager+RadioGroup实现底部导航栏和页面的滑动
- Codeforces Round #403 C. Andryusha and Colored Balloons
- 一起Talk Android吧(第十一回:Java中的继承)
- 520. Detect Capital
- POJ1001
- Vue上传图片预览以及删除的vue组件
- 有关计算机体系结构和人工智能的随想
- grpc(5):使用grpc+consul 开发服务调用
- 链表实现两个多项式的加法
- java中的final