Codeforces Round #403 (Div. 1, based on Technocup 2017 Finals) A. Andryusha and Colored Balloons
来源:互联网 发布:怎么在淘宝上注册开店 编辑:程序博客网 时间:2024/06/05 16:51
题目分析
这道题需要求的要多少种颜色,很明显就是一个节点连接的边数最大值加一。这样再dfs进行染色,注意要保证该点与父节点和父父节点不一样,然后就可以了,不明白的直接看代码。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 2e5+100;struct Edge{ int to, next;}e[maxn<<1];int head[maxn], degree[maxn], color[maxn], tot;void addedge(int from, int to){ e[tot].to = to; e[tot].next = head[from]; head[from] = tot++;}void dfs(int u, int fa, int ffa){ int tot = 1; for(int i = head[u]; i != -1; i = e[i].next){ int v = e[i].to; if(!color[v]){ if(fa == tot){ ++tot; if(ffa == tot) ++tot; } if(ffa == tot){ ++tot; if(fa == tot) ++tot; } color[v] = tot++; dfs(v, color[v], color[u]); } }}void init(){ memset(color, 0, sizeof(color)); memset(degree, 0, sizeof(degree)); memset(head, -1, sizeof(head)); tot = 0;}int main(){ #ifdef LOCAL freopen("input.txt", "r", stdin); #endif // LOCAL int n; while(scanf("%d", &n) != EOF){ int from, to; init(); for(int i = 1; i < n; i++){ scanf("%d%d", &from, &to); addedge(from, to); addedge(to, from); degree[from]++; degree[to]++; } int ans = 0, temp; for(int i = 1; i <= n; i++) if(degree[i]+1 > ans){ temp = i; ans = max(ans, degree[i]+1); } printf("%d\n", ans); color[temp] = 1; dfs(temp, 1, -1); for(int i = 1; i <= n; i++) printf("%d ", color[i]); printf("\n"); } return 0;}
0 0
- Codeforces Round #403 (Div. 1, based on Technocup 2017 Finals) A. Andryusha and Colored Balloons
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)【A,B,C】
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)A,B,C
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) A.B.C
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) A
- 【Codeforces Round #403】Codeforces 781A Andryusha and Colored Balloons
- Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons
- Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons
- [Updating]Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)解题报告
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) B.(三分)
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) B.(三分)
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C (dfs+vector)
- 【解题报告】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C
- Codeforces Round #403 C. Andryusha and Colored Balloons(DFS)
- 按之字形打印二叉树的节点【两个栈实现】
- ajax学习笔记
- leetcode#101 Symmetric Tree
- JAVA第三大特性-----多态
- 友善网
- Codeforces Round #403 (Div. 1, based on Technocup 2017 Finals) A. Andryusha and Colored Balloons
- 求0到n之间所有数字中1的个数和
- HDU4812:D Tree
- CCF 201512-1
- QThread中的互斥、读写锁、信号量、条件变量
- L1-030. 一帮一
- node.js学习(六、express创建web服务器)
- L3-011. 直捣黄龙
- 科普:手机定位的原理