嘟嘟嘟




水题一道,某谷又恶意评分。




合并无非是将两棵树的直径的中点连一块,记原来两棵树的直径为\(d_1, d_2\),那么新的树的直径就是\(max(d_1, d_2, \lceil \frac{d_1}{2} \rceil + \lceil \frac{d_2}{2} \rceil + 1)\),用并查集合并,更新即可。

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cmath>
  4. #include<algorithm>
  5. #include<cstring>
  6. #include<cstdlib>
  7. #include<cctype>
  8. #include<vector>
  9. #include<stack>
  10. #include<queue>
  11. #include<assert.h>
  12. using namespace std;
  13. #define enter puts("")
  14. #define space putchar(' ')
  15. #define Mem(a, x) memset(a, x, sizeof(a))
  16. #define In inline
  17. typedef long long ll;
  18. typedef double db;
  19. const int INF = 0x3f3f3f3f;
  20. const db eps = 1e-8;
  21. const int maxn = 3e5 + 5;
  22. In ll read()
  23. {
  24. ll ans = 0;
  25. char ch = getchar(), last = ' ';
  26. while(!isdigit(ch)) last = ch, ch = getchar();
  27. while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  28. if(last == '-') ans = -ans;
  29. return ans;
  30. }
  31. In void write(ll x)
  32. {
  33. if(x < 0) x = -x, putchar('-');
  34. if(x >= 10) write(x / 10);
  35. putchar(x % 10 + '0');
  36. }
  37. In void MYFILE()
  38. {
  39. #ifndef mrclr
  40. freopen(".in", "r", stdin);
  41. freopen(".out", "w", stdout);
  42. #endif
  43. }
  44. int n, m, Q;
  45. struct Edge
  46. {
  47. int nxt, to;
  48. }e[maxn << 1];
  49. int head[maxn], ecnt = -1;
  50. In void addEdge(int x, int y)
  51. {
  52. e[++ecnt] = (Edge){head[x], y};
  53. head[x] = ecnt;
  54. }
  55. int p[maxn];
  56. In int Find(int x) {return x == p[x] ? x : p[x] = Find(p[x]);}
  57. int Max[maxn], dia[maxn];
  58. In void dfs(int now, int _f, int id)
  59. {
  60. p[now] = id;
  61. int Max1 = 0, Max2 = 0;
  62. for(int i = head[now], v; ~i; i = e[i].nxt)
  63. {
  64. if((v = e[i].to) == _f) continue;
  65. dfs(v, now, id);
  66. if(Max[v] + 1 > Max1) Max2 = Max1, Max1 = Max[v] + 1;
  67. else if(Max[v] + 1 > Max2) Max2 = Max[v] + 1;
  68. }
  69. Max[now] = Max1;
  70. dia[id] = max(dia[id], Max1 + Max2);
  71. }
  72. int main()
  73. {
  74. //MYFILE();
  75. Mem(head, -1);
  76. n = read(), m = read(), Q = read();
  77. for(int i = 1; i <= m; ++i)
  78. {
  79. int x = read(), y = read();
  80. addEdge(x, y), addEdge(y, x);
  81. }
  82. for(int i = 1; i <= n; ++i) if(!p[i]) dfs(i, 0, i);
  83. for(int i = 1; i <= Q; ++i)
  84. {
  85. int op = read(), x = read();
  86. if(op == 1) write(dia[Find(x)]), enter;
  87. else
  88. {
  89. int y = read();
  90. int px = Find(x), py = Find(y);
  91. if(px == py) continue;
  92. p[px] = py;
  93. dia[py] = max(max(dia[px], dia[py]), (dia[px] + 1) / 2 + (dia[py] + 1) / 2 + 1);
  94. }
  95. }
  96. return 0;
  97. }

CF455C Civilization的更多相关文章

  1. CF455C Civilization (并查集)

    CF456E Codeforces Round #260 (Div. 1) C Codeforces Round #260 (Div. 2) E http://codeforces.com/conte ...

  2. cf455C Civilization (并查集)

    并查集维护每个联通块的直径和最小的最大深度,每次连得时候连的肯定是最大深度最小的那两个点 #pragma GCC optimize(3) #include<bits/stdc++.h> # ...

  3. CF455C Civilization | luogu HXY造公园

    题目链接: https://www.luogu.org/problemnew/show/P2195 http://codeforces.com/contest/455/problem/C 显然我们可以 ...

  4. CF455C Civilization 树的直径

    问题描述 LG-CF455C 题解 首先,题目给出了 \(m\) 条边,对这 \(n\) 个点, \(m\) 条边组成的森林,跑出每棵树的直径,同时使用并查集维护树的连通性. 考虑合并两棵树的情况:设 ...

  5. NOIP前刷题记录

    因为本蒻实在太蒻了...对于即将到来的NOIP2018ssfd,所以下决心要把自己近期做过的题目(衡量标准为洛谷蓝题难度或以上)整理一下,归归类,简单地写一下思路,就当作自己复习了吧qwq 本随笔持续 ...

  6. NOIP刷题

    搜索 [NOIP2013]华容道 最短路+带剪枝的搜索,是一个思维难度比较大的题目. CF1064D Labyrinth 考虑贪心,用双向队列bfs [NOIP2017]宝藏 剪枝搜索出奇迹 题解:h ...

  7. Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径

    C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...

  8. CodeForces 455C Civilization (并查集+树的直径)

    Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a ...

  9. Codeforces Round #260 (Div. 1) C. Civilization 树的中心+并查集

    题目链接: 题目 C. Civilization time limit per test1 second memory limit per test256 megabytes inputstandar ...

随机推荐

  1. 细说浏览器输入URL后发生了什么

    本文摘要: 1.DNS域名解析: 2.建立TCP连接: 3.发送HTTP请求: 4.服务器处理请求: 5.返回响应结果: 6.关闭TCP连接: 7.浏览器解析HTML: 8.浏览器布局渲染: 总结   ...

  2. Scratch编程:多彩的舞台(六)

    “ 上节课的内容全部掌握了吗?反复练习了没有,编程最好的学习方法就是练习.练习.再练习.一定要记得多动手.多动脑筋哦~~” 01 — 游戏介绍 这是一款简单的小游戏,实现了一个小女孩在多彩的舞台上进行 ...

  3. 从入门到精通,Java学习路线导航

    引言最近也有很多人来向我"请教",他们大都是一些刚入门的新手,还不了解这个行业,也不知道从何学起,开始的时候非常迷茫,实在是每天回复很多人也很麻烦,所以在这里统一作个回复吧. Ja ...

  4. 两通道实信号使用一个FFT同时计算算法

    前言 在工程的实际应用场景中,往往是需要最省资源量.而DSP资源和BRAM资源对FPGA来说弥足珍贵. 对于同时存在多个通道的实信号需要做FFT而言,常规做法是每个通道用一个FFT IP,FFT IP ...

  5. stm32 内部flash

    嵌入式闪存 闪存存储器有主存储块和信息块组成 大容量产品主存储块最大为64K×64位,每个存储块划分为256个2K字节的页 编程和擦除闪存 闪存编程一次可以写入16位(半字) 闪存擦除操作可以按页面擦 ...

  6. Xcode11.1 踩坑备忘录

    Xcode11.1 踩坑备忘录(mac系统10.15) 1 .环信ChatDemo2.0报错 这是环信ChatDemo2.0报错 NSInteger numberOfBeforeSection = [ ...

  7. 阿里云给自己实例扩容-扩展分区和文件系统_Linux系统盘

    阿里云买了台服务器ecs 磁盘容量40g 发现已经用了30g了  赶紧扩容 进入 e'cs实例 进入左边菜单 存储与快照 然后选择右边的扩容 然后支付 成功后 进入服务器 df -h 发现怎么还是没变 ...

  8. JAVA笔记整理(六),JAVA中的多态

    JAVA引用变量有两个类型:一个是编译时类型,一个运行时类型 编译时类型由声明该变量时使用的类型决定,运行时类型由实际赋给该变量的对象决定.如果编译时类型和运行时类型不一样,就形成了多态. 因为子类其 ...

  9. springboot知识点【笔记】

    # **一.**Spring Boot 入门 ## 1.Spring Boot 简介 > 简化Spring应用开发的一个框架:>> 整个Spring技术栈的一个大整合:>> ...

  10. centos7 修改内核文件 网卡名称为标准名称eth0

    在开机安装系统之前按TAB键后输入标记信息后安装系统就可以变成标准网卡接口eth0 或eth1