http://codeforces.com/contest/734/problem/E

看了题解,缩点 + 树的直径。

然而一直wa14.

注意到,

缩点后重建图,在5的时候,5和6建了一条边,然后6的时候,又和5建一次边。这个时候就要大数组了。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = * + ;
int first[maxn];
int first_sec[maxn];
int num, num_sec;
struct node {
int u, v, w;
int tonext;
}e[maxn * ], e_sec[maxn * ];
int col[maxn];
void add(int u, int v) {
++num;
e[num].u = u;
e[num].v = v;
e[num].tonext = first[u];
first[u] = num;
}
void add_sec(int u, int v) {
++num_sec;
e_sec[num_sec].u = u;
e_sec[num_sec].v = v;
e_sec[num_sec].tonext = first_sec[u];
first_sec[u] = num_sec;
}
bool vis[maxn];
int fa[maxn];
int find(int u) {
if (u == fa[u]) return u;
else return fa[u] = find(fa[u]);
}
void merge(int u, int v) {
u = find(u);
v = find(v);
if (u != v) {
fa[v] = u;
}
}
void dfs(int cur) {
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (vis[v]) continue;
vis[v] = true;
if (col[cur] == col[v]) {
merge(cur, v);
}
dfs(v);
}
}
void dfs2(int cur) {
printf("cur %d : %d\n", cur, col[cur]);
for (int i = first_sec[cur]; i; i = e_sec[i].tonext) {
int v = e_sec[i].v;
if (vis[v]) continue;
vis[v] = true;
dfs2(v);
}
}
struct NODE {
int cur, cnt;
NODE(int aa, int bb) : cur(aa), cnt(bb) {}
};
int bfs(int begin) {
memset(vis, , sizeof vis);
queue<struct NODE>que;
while(!que.empty()) que.pop();
que.push(NODE(begin, ));
vis[begin] = true;
int mx = -inf;
int to = begin;
while (!que.empty()) {
struct NODE t = que.front();
que.pop();
for (int i = first_sec[t.cur]; i; i = e_sec[i].tonext) {
int v = e_sec[i].v;
if (vis[v]) continue;
que.push(NODE(v, t.cnt + ));
vis[v] = true;
if (mx < t.cnt + ) {
mx = t.cnt + ;
to = v;
}
}
}
if (to == begin) return ;
// cout << to << " " << mx << endl;
que.push(NODE(to, ));
memset(vis, , sizeof vis);
vis[to] = true;
mx = -inf;
to = ;
while (!que.empty()) {
struct NODE t = que.front();
que.pop();
for (int i = first_sec[t.cur]; i; i = e_sec[i].tonext) {
int v = e_sec[i].v;
if (vis[v]) continue;
que.push(NODE(v, t.cnt + ));
if (mx < t.cnt + ) {
mx = t.cnt + ;
to = v;
}
vis[v] = true;
}
}
// cout << mx << " " << to << endl;
return mx;
}
void work() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) fa[i] = i;
for (int i = ; i <= n; ++i) scanf("%d", &col[i]);
for (int i = ; i <= n - ; ++i) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
if (num > * ) while();
}
vis[] = true;
dfs();
// for (int i = 1; i <= n; ++i) {
// printf("%d\n", find(i));
// }
for (int i = ; i <= n; ++i) {
for (int j = first[i]; j; j = e[j].tonext) {
int v = e[j].v;
if (find(i) == find(v)) continue;
// printf("%d %d\n", find(i), find(v));
// printf("fff");
add_sec(find(i), find(v));
add_sec(find(v), find(i));
}
}
// memset(vis, 0, sizeof vis);
// vis[1] = 1;
// dfs2(1);
int ans = bfs(find());
cout << (ans + ) / << endl;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}

为什么直径 /  2是答案?因为可以在直径中间的那个点,一直变一直变。

E. Anton and Tree 数组开大点的更多相关文章

  1. [ACM_数据结构] Color the ball [线段树水题][数组开大]

    Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次 ...

  2. Atitit 编程语言知识点tech tree v2 attilax大总结

    Atitit 编程语言知识点tech tree v2 attilax大总结 大分类中分类小分类知识点原理与规范具体实现(javac#里面的实现phpjsdsl(自己实现其他语言实现 类与对象实现对象实 ...

  3. Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路

    题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...

  4. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  5. Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径

    E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...

  6. Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径

    E. Anton and Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...

  7. 开大Stack的一个小技巧

    在程序头部添加一行 #pragma comment(linker, "/STACK:16777216") 可有效开大堆栈 实验效果如下: 11330179 2014-08-05 1 ...

  8. Anton and Tree

    Anton and Tree 题目链接:http://codeforces.com/contest/734/problem/E DFS/BFS 每一次操作都可以使连通的结点变色,所以可以将连通的点缩成 ...

  9. Codeforces 734E Anton and Tree(缩点+树的直径)

    题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...

随机推荐

  1. List 调用 remove 方法时抛出 java.lang.UnsupportedOperationException 异常原因

    原因 使用 Arrays.asList(arr) 转换的 List 并不能进行 add 和 remove 操作.       Arrays.asList(arr) 返回的类型是 Aarrays$Arr ...

  2. Axure Base 09 带遮罩层的弹出框

    示例原型下载:小楼Axure原创元件-带遮罩层的弹出框 实现目标: 1.   点击按钮弹出带遮罩层的对话框: 2.   页面上下左右滚动时,弹出的对话框水平和垂直始终居中. 实现步骤如下: 1. 拖入 ...

  3. (linux)container_of()宏

      在学习Linux驱动的过程中,遇到一个宏叫做container_of. 该宏定义在include/linux/kernel.h中,首先来贴出它的代码: /**  * container_of - ...

  4. android user用户版本提高adb权限【转】

    本文转载自:http://blog.csdn.net/liyongming1982/article/details/14108111 有的user用户版本的log 不全,且push/pull某些文件或 ...

  5. loadrunner性能测试步骤

    性能测试过程分为4个阶段:设计.构建.执行.分析/诊断/调节具体的工作流程如下图 设计 > 构建 > 执行 > 分析/诊断/调节 收集要求 设置测试环境 基准测试 诊断瓶颈 设计测试 ...

  6. html5--6-11 CSS选择器7--伪类选择器

    html5--6-11 CSS选择器7--伪类选择器 #E:target 选择当前活动的锚点元素. 学习要点 掌握常用的CSS选择器 了解不太常用的CSS选择器 什么是选择器 当我们定义一条样式时候, ...

  7. RecyclerView 局部刷新(获取viewHolder 去刷新)

    RecyclerView.ViewHolder viewHolder = mRecyclerView.findViewHolderForAdapterPosition(i); if (viewHold ...

  8. C#方法参数总结

    C#中方法的参数的四种类型 C#中方法的参数有四种类型:       1. 值参数类型  (不加任何修饰符,是默认的类型)       2. 引用型参数  (以ref 修饰符声明)       3. ...

  9. 在Service里调用AlertDialog

    用常规的方法在AlertDialog的时候,会报错,大意是「can not add window in this view」. 原因是Service是没有界面的,只有Activity才能添加界面. 解 ...

  10. javascript之常遇到的浏览器兼容问题和解决方法

    转自http://www.cnblogs.com/duenyang/p/6066737.html 常遇到的关于浏览器的宽高问题: var winW=document.body.clientWidth| ...