Codeforces734E【缩点+树的直径】
来源:互联网 发布:mac虚拟机驱动 编辑:程序博客网 时间:2024/06/06 03:35
思路:
想不到树的直径就GG了啊…
先缩点,然后缩成一棵相邻两层节点就是不同的个数,然后如果考虑一棵树是
1-0-1-0-1-0-1-0-1-0
这样的话其实就是中间开始搞,然后最少变node_num/2次变成同一种颜色。
然后考虑一棵复杂的树,当树上最长距离像这样在变的时候其余枝干肯定是会被连带到的!
//#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;typedef long long LL;#define mem(a, b) memset(a, b, sizeof(a))const int Maxn = 2e5 + 10;struct Edge{ int v, nex;}edge[Maxn<<1], e[Maxn<<1];int head[Maxn], tol, op[Maxn], tal;int val[Maxn], tot, col[Maxn], be[Maxn];int n;void init(){ tol = tot = tal = 0; memset(op, -1, sizeof(op)); memset(head, -1, sizeof(head));}void add(int u, int v){ edge[tol] = (Edge){v, head[u]}, head[u] = tol++; edge[tol] = (Edge){u, head[v]}, head[v] = tol++;}void ADD(int u, int v){ e[tal] = (Edge){v, op[u]}, op[u] = tal++; e[tal] = (Edge){u, op[v]}, op[v] = tal++;}void DFS(int u, int fa){ int v; if(fa != -1){ if(col[be[fa]] != val[u]){ be[u] = ++tot; col[be[u]] = val[u]; ADD(be[fa], tot); } else be[u] = be[fa]; } for(int i=head[u]; ~i; i = edge[i].nex){ v = edge[i].v; if(v == fa) continue; DFS(v, u); }}int Mmmax(int x, int y, int z){ x = x > y ? x : y; return x > z ? x : z;}int dis[Maxn];int ans;void DFS_LEN(int u, int fa, int pre_num){ int v; int temp, Max1, Max2; Max1 = Max2 = 0; dis[u] = 1; for(int i=op[u]; ~i; i=e[i].nex){ v = e[i].v; if(v == fa) continue; temp = Mmmax(Max1, Max2, pre_num); DFS_LEN(v, u, temp); if(dis[v] > Max1){ Max2 = Max1; Max1 = dis[v]; } else if(dis[v] > Max2) Max2 = dis[v]; } dis[u] = dis[u] + Max1; ans = Mmmax(ans, Max1+Max2, Max1+pre_num);}int main(){ int u, v; scanf("%d", &n); for(int i=1;i<=n;i++) scanf("%d", &val[i]); init(); for(int i=1;i<n;i++){ scanf("%d%d", &u, &v); add(u, v); } col[++tot] = val[1]; be[1] = tot; DFS(1, -1); ans = 0; DFS_LEN(1, -1, 0); printf("%d\n", (ans+1)/2); return 0;}
阅读全文
0 0
- Codeforces734E【缩点+树的直径】
- [Codeforces734E]Anton and Tree 树的直径
- 【CodeForces734E】【缩点】Anton and Tree 题解
- hdu4612(缩点+树的直径)
- HDU 4612(缩点+求桥+求树的直径)
- hdu4612(双联通缩点+求树的直径)
- Gym 100712HBridges(桥+缩点+树的直径)
- CodeForces734E
- hdu 4612 Warm up(边-双连通+缩点+树的直径)
- Warm up(hdu4612双连通分量+缩点+树的直径)
- HDU 4612 Warm up (缩点+树的直径,有重边)
- Codeforces 734E. Anton and Tree By Assassin 缩点+树的最大直径
- Warm up 【tarjan 求EBC+求桥+缩点+树的直径】
- Codeforces 734E Anton and Tree (dfs缩点+树的直径)
- Codeforces 734E Anton and Tree【并查集缩点||DFS缩点+树的直径】
- hdu 4612 Warm up 双连通缩点+树的直径
- Hdu 4612 Warm up (双连通缩点+树的直径)
- hdu4612Warm up(双联通缩点+树直径)
- Java进阶之路——从初级程序员到架构师,从小工到专家
- 013.单链队列基本操作C实例
- 树的前序、中序、后序遍历
- java中打印数组的几种方法
- #undef
- Codeforces734E【缩点+树的直径】
- 初识Lambda表达式
- CountDownLatch
- 445. Add Two Numbers II
- python迭代器和生成器
- Linux下C语言使用openssl库进行MD5校验
- 模板和泛型编程
- SQL字段拼接显示
- 解决poi导出excel中文列宽问题