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

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

然而一直wa14.

注意到,

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

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #define IOS ios::sync_with_stdio(false)
  7. using namespace std;
  8. #define inf (0x3f3f3f3f)
  9. typedef long long int LL;
  10.  
  11. #include <iostream>
  12. #include <sstream>
  13. #include <vector>
  14. #include <set>
  15. #include <map>
  16. #include <queue>
  17. #include <string>
  18. const int maxn = * + ;
  19. int first[maxn];
  20. int first_sec[maxn];
  21. int num, num_sec;
  22. struct node {
  23. int u, v, w;
  24. int tonext;
  25. }e[maxn * ], e_sec[maxn * ];
  26. int col[maxn];
  27. void add(int u, int v) {
  28. ++num;
  29. e[num].u = u;
  30. e[num].v = v;
  31. e[num].tonext = first[u];
  32. first[u] = num;
  33. }
  34. void add_sec(int u, int v) {
  35. ++num_sec;
  36. e_sec[num_sec].u = u;
  37. e_sec[num_sec].v = v;
  38. e_sec[num_sec].tonext = first_sec[u];
  39. first_sec[u] = num_sec;
  40. }
  41. bool vis[maxn];
  42. int fa[maxn];
  43. int find(int u) {
  44. if (u == fa[u]) return u;
  45. else return fa[u] = find(fa[u]);
  46. }
  47. void merge(int u, int v) {
  48. u = find(u);
  49. v = find(v);
  50. if (u != v) {
  51. fa[v] = u;
  52. }
  53. }
  54. void dfs(int cur) {
  55. for (int i = first[cur]; i; i = e[i].tonext) {
  56. int v = e[i].v;
  57. if (vis[v]) continue;
  58. vis[v] = true;
  59. if (col[cur] == col[v]) {
  60. merge(cur, v);
  61. }
  62. dfs(v);
  63. }
  64. }
  65. void dfs2(int cur) {
  66. printf("cur %d : %d\n", cur, col[cur]);
  67. for (int i = first_sec[cur]; i; i = e_sec[i].tonext) {
  68. int v = e_sec[i].v;
  69. if (vis[v]) continue;
  70. vis[v] = true;
  71. dfs2(v);
  72. }
  73. }
  74. struct NODE {
  75. int cur, cnt;
  76. NODE(int aa, int bb) : cur(aa), cnt(bb) {}
  77. };
  78. int bfs(int begin) {
  79. memset(vis, , sizeof vis);
  80. queue<struct NODE>que;
  81. while(!que.empty()) que.pop();
  82. que.push(NODE(begin, ));
  83. vis[begin] = true;
  84. int mx = -inf;
  85. int to = begin;
  86. while (!que.empty()) {
  87. struct NODE t = que.front();
  88. que.pop();
  89. for (int i = first_sec[t.cur]; i; i = e_sec[i].tonext) {
  90. int v = e_sec[i].v;
  91. if (vis[v]) continue;
  92. que.push(NODE(v, t.cnt + ));
  93. vis[v] = true;
  94. if (mx < t.cnt + ) {
  95. mx = t.cnt + ;
  96. to = v;
  97. }
  98. }
  99. }
  100. if (to == begin) return ;
  101. // cout << to << " " << mx << endl;
  102. que.push(NODE(to, ));
  103. memset(vis, , sizeof vis);
  104. vis[to] = true;
  105. mx = -inf;
  106. to = ;
  107. while (!que.empty()) {
  108. struct NODE t = que.front();
  109. que.pop();
  110. for (int i = first_sec[t.cur]; i; i = e_sec[i].tonext) {
  111. int v = e_sec[i].v;
  112. if (vis[v]) continue;
  113. que.push(NODE(v, t.cnt + ));
  114. if (mx < t.cnt + ) {
  115. mx = t.cnt + ;
  116. to = v;
  117. }
  118. vis[v] = true;
  119. }
  120. }
  121. // cout << mx << " " << to << endl;
  122. return mx;
  123. }
  124. void work() {
  125. int n;
  126. scanf("%d", &n);
  127. for (int i = ; i <= n; ++i) fa[i] = i;
  128. for (int i = ; i <= n; ++i) scanf("%d", &col[i]);
  129. for (int i = ; i <= n - ; ++i) {
  130. int u, v;
  131. scanf("%d%d", &u, &v);
  132. add(u, v);
  133. add(v, u);
  134. if (num > * ) while();
  135. }
  136. vis[] = true;
  137. dfs();
  138. // for (int i = 1; i <= n; ++i) {
  139. // printf("%d\n", find(i));
  140. // }
  141. for (int i = ; i <= n; ++i) {
  142. for (int j = first[i]; j; j = e[j].tonext) {
  143. int v = e[j].v;
  144. if (find(i) == find(v)) continue;
  145. // printf("%d %d\n", find(i), find(v));
  146. // printf("fff");
  147. add_sec(find(i), find(v));
  148. add_sec(find(v), find(i));
  149. }
  150. }
  151. // memset(vis, 0, sizeof vis);
  152. // vis[1] = 1;
  153. // dfs2(1);
  154. int ans = bfs(find());
  155. cout << (ans + ) / << endl;
  156. }
  157.  
  158. int main() {
  159. #ifdef local
  160. freopen("data.txt","r",stdin);
  161. #endif
  162. work();
  163. return ;
  164. }

为什么直径 /  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. MapReduce算法形式三:cleanup

    案例三:cleanup 其实这个案例可以不用写这么复杂,不用cleanup也能写,但是为了,突显,突显,突显(重要的事说四遍)cleanup的重要性,琢磨了半天,恩,这样写既可以突显cleanup又显 ...

  2. react native 知识点总结(一)

    一.关于react native 版本的升级 参照文档:http://reactnative.cn/docs/0.45/upgrading.html react-native -v   查看当前版本 ...

  3. jQuery常用插件大全(9)ResponsiveSlides插件

    ResponsiveSlides.js是一个展示同一容器内图片的轻量级响应式jQuery幻灯片插件(tiny responsive slideshow jQuery plugin).它支持包括IE6在 ...

  4. SpringBoot发送简单文本邮件

    1.pom.xml添加 spring-boot-starter-mail 依赖 <dependency> <groupId>org.springframework.boot&l ...

  5. CSS自定义文件上传按钮样式,兼容主流浏览器

    解决办法:使用text文本框及a链接模拟文件上传按钮,并且把文件上传按钮放在他们上面,并且文件上传按钮显示透明.​1.图片​​2. [代码][HTML]代码 <div class="b ...

  6. ES6 模板编译

    顾名思义,就是用反引号编写一个模板字符串, 用echo将模板转为javascrip表达式字符串, 用正则将基础字符串转为想要字符串 将代码封装在函数中返回: 注: 用到es6属性${} var tem ...

  7. saltstack自动化运维快速入门

    saltstack自动化运维快速入门 关于saltstack 这个软件是干啥的 我这里就不介绍了 只是简单的说下是干啥的 网上的说法是 它是func的强化版本+ puppet的精简版 关于puppet ...

  8. CS231n 2016 通关 第一章-内容介绍

    第一节视频的主要内容: Fei-Fei Li 女神对Computer Vision的整体介绍.包括了发展历史中的重要事件,其中最为重要的是1959年测试猫视觉神经的实验. In 1959 Harvar ...

  9. UI:动画

    参考 UIView 层级管理.触摸.检测手机是否横屏.调整设备的方向 动画:为了提高用户的体验 View层的动画.UIlayer层的动画.UIView改变视图效果.UIlayer的绘图框架 #prag ...

  10. In-App Purchase Programming Guide----(五) ----Delivering Products

    Delivering Products In the final part of the purchase process, your app waits for the App Store to p ...