Codeforces-734E Anton and Tree(树的直径)
来源:互联网 发布:usr share mysql 编辑:程序博客网 时间:2024/06/05 16:42
Anton is growing a tree in his garden. In case you forgot, the tree is a connected acyclic undirected graph.
There are n vertices in the tree, each of them is painted black or white. Anton doesn't like multicolored trees, so he wants to change the tree such that all vertices have the same color (black or white).
To change the colors Anton can use only operations of one type. We denote it as paint(v), where v is some vertex of the tree. This operation changes the color of all vertices u such that all vertices on the shortest path from v to u have the same color (including v andu). For example, consider the tree
and apply operation paint(3) to get the following:
Anton is interested in the minimum number of operation he needs to perform in order to make the colors of all vertices equal.
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree.
The second line contains n integers colori (0 ≤ colori ≤ 1) — colors of the vertices. colori = 0 means that the i-th vertex is initially painted white, while colori = 1 means it's initially painted black.
Then follow n - 1 line, each of them contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — indices of vertices connected by the corresponding edge. It's guaranteed that all pairs (ui, vi) are distinct, i.e. there are no multiple edges.
Print one integer — the minimum number of operations Anton has to apply in order to make all vertices of the tree black or all vertices of the tree white.
110 0 0 1 1 0 1 0 0 1 11 21 32 42 55 65 73 83 93 109 11
2
40 0 0 01 22 33 4
0
In the first sample, the tree is the same as on the picture. If we first apply operation paint(3) and then apply paint(6), the tree will become completely black, so the answer is 2.
In the second sample, the tree is already white, so there is no need to apply any operations and the answer is 0.
题解:把所有颜色相同且相连的点缩成一个点,然后重新构建树,可以发现黑白是相隔出现的,只要找出树的直径,把直径中心的点连续变换len/2次即可让所有的节点颜色相同,len为直径长度
#include<bits/stdc++.h>#define x first#define y secondusing namespace std;typedef pair<int, int> PII;typedef long long LL;const int MX = 2e5 + 5;const int inf = 0x3f3f3f3f;struct Edge { int v, nxt;} E[MX * 2];int head[MX], tot;void init() { memset(head, -1, sizeof(head)); tot = 0;}void add(int u, int v) { E[tot].v = v; E[tot].nxt = head[u]; head[u] = tot++;}int n, vis[MX], c[MX], p[MX];void dfs(int u, int rt) { vis[u] = 1; p[u] = rt; for (int i = head[u]; ~i; i = E[i].nxt) { int v = E[i].v; if (c[v] != c[u] || vis[v]) continue; dfs(v, rt); }}int dep[MX];void DFS(int u, int fa) { for (int i = head[u]; ~i; i = E[i].nxt) { int v = E[i] .v; if (v == fa) continue; dep[v] = dep[u] + 1; DFS(v, u); }}PII e[MX];int main() { //freopen("in.txt", "r", stdin); init(); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &c[i]); for (int i = 1; i < n; i++) { scanf("%d%d", &e[i].x, &e[i].y); add(e[i].x, e[i].y); add(e[i].y, e[i].x); } memset(vis,0,sizeof(vis)); for (int i = 1; i <= n; i++) if (!vis[i]) dfs(i, i); init(); for (int i = 1; i < n; i++) { if (p[e[i].x] != p[e[i].y]) { add(p[e[i].x], p[e[i].y]); add(p[e[i].y], p[e[i].x]); } } int rt = p[1]; dep[rt] = 1; DFS(rt, -1); for (int i = 1; i <= n; i++) if (p[i] == i && dep[i] > dep[rt]) rt = i; int mx = 0; dep[rt] = 1; DFS(rt, -1); for (int i = 1; i <= n; i++) if (p[i] == i && dep[i] > mx) mx = dep[i]; //printf("%d\n",mx); printf("%d\n", mx / 2); return 0;}
- Codeforces-734E Anton and Tree(树的直径)
- Codeforces 734E Anton and Tree (dfs缩点+树的直径)
- Codeforces 734E. Anton and Tree By Assassin 缩点+树的最大直径
- Codeforces 734E Anton and Tree【并查集缩点+树的直径】好题~
- Codeforces 734E Anton and Tree【并查集缩点||DFS缩点+树的直径】
- 思路题,树的直径(Anton and Tree,cf 734E)
- [Codeforces734E]Anton and Tree 树的直径
- CodeForces 734E - Anton and Tree
- 【27.91%】【codeforces 734E】Anton and Tree
- E. Anton and Tree(缩点+树直径模板)
- CodeForces 734 E.Anton and Tree(dfs)
- 【codeforces 734E】Anton and Tree【缩点+DP】
- codeforces 734 E. Anton and Tree (黑红树,缩点+dfs)
- Anton and Tree CodeForces
- CF734E:Anton and Tree(缩点 & 直径)
- codeforces 785 E. Anton and Permutation(分块)
- CodeForces 785E Anton and Permutation (分块)
- Codeforces 584E Anton and Ira
- 函数式编程
- Fragment 生命周期完整版
- C++(7)模板与泛型编程
- MySQL性能分析之explain的使用
- CodeBlock一些使用技巧
- Codeforces-734E Anton and Tree(树的直径)
- (转)聪明的券商都在加大IT投入
- SpringMVC的RequestContextHolder详解分析
- NYOJ-119-士兵杀敌(三)
- 2017-8-12
- HDU 6059 Kanade's trio (字典树)
- 修理牧场
- 190
- 191