嘟嘟嘟




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




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

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<assert.h>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 3e5 + 5;
In ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
In void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
} int n, m, Q;
struct Edge
{
int nxt, to;
}e[maxn << 1];
int head[maxn], ecnt = -1;
In void addEdge(int x, int y)
{
e[++ecnt] = (Edge){head[x], y};
head[x] = ecnt;
} int p[maxn];
In int Find(int x) {return x == p[x] ? x : p[x] = Find(p[x]);} int Max[maxn], dia[maxn];
In void dfs(int now, int _f, int id)
{
p[now] = id;
int Max1 = 0, Max2 = 0;
for(int i = head[now], v; ~i; i = e[i].nxt)
{
if((v = e[i].to) == _f) continue;
dfs(v, now, id);
if(Max[v] + 1 > Max1) Max2 = Max1, Max1 = Max[v] + 1;
else if(Max[v] + 1 > Max2) Max2 = Max[v] + 1;
}
Max[now] = Max1;
dia[id] = max(dia[id], Max1 + Max2);
} int main()
{
//MYFILE();
Mem(head, -1);
n = read(), m = read(), Q = read();
for(int i = 1; i <= m; ++i)
{
int x = read(), y = read();
addEdge(x, y), addEdge(y, x);
}
for(int i = 1; i <= n; ++i) if(!p[i]) dfs(i, 0, i);
for(int i = 1; i <= Q; ++i)
{
int op = read(), x = read();
if(op == 1) write(dia[Find(x)]), enter;
else
{
int y = read();
int px = Find(x), py = Find(y);
if(px == py) continue;
p[px] = py;
dia[py] = max(max(dia[px], dia[py]), (dia[px] + 1) / 2 + (dia[py] + 1) / 2 + 1);
}
}
return 0;
}

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. Java基础IO类之数据流

    DataInputStream: 数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本java数据类型.应用程序可以使用数据输出流 写入稍后由数据输入流读取的数据.DataInputStrea ...

  2. ASP.Net Core 返回的json数据,自定义日期格式

    //代码位置:Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddMvc() .Ad ...

  3. [BZOJ3681]Arietta(可持久化线段树合并优化建图+网络流)

    暴力建图显然就是S->i连1,i->j'连inf(i为第j个力度能弹出的音符),j'->T连T[j]. 由于是“某棵子树中权值在某区间内的所有点”都向某个力度连边,于是线段树优化建图 ...

  4. 数据结构之队列(queue)

    队列介绍 1.队列是一个有序列表,可以用数组或是链表来实现. 2.遵循先入先出的原则.即:先存入队列的数据,要先取出.后存入的要后取出. 应用场景 比如某某银行叫号系统: 数组模拟队列 队列本身是有序 ...

  5. VUE 元素拖拽、移动

    元素拖拽 作者:一粒尘土 时间:2019-10-30 使用范围:两个元素位置交换,移动元素到指定位置 涉及函数 属性 解释 draggable 是否允许元素进行拖拽 dragstart 拖拽开始触发的 ...

  6. K2 BPM_加班党们,说好不哭还是说好不秃?_流程管理

    早上经过财务小陈的办公桌 看到她正边看手机边默默流泪 诶?这不是这两天刷屏的 周杰伦的<说好不哭>吗 小陈你怎么哭啦,这歌让你想到前男友了吗? ...... (摇头) 小陈擦擦眼泪 唱起了 ...

  7. oracle trunc函数用法

    转自:https://www.e-learn.cn/content/qita/699481 /**************日期********************/ select trunc(sy ...

  8. FreeRTOS 移植

    添加FreeRTOS源码到工程中 在工程源码中创建FreeRTOS目录存放拷贝的文件 拷贝FreeRTOS->Source中的文件 可将其他不需要的文件夹全部删掉,只留3个 拷贝Demo中Fre ...

  9. 使用browser-sync预览pandoc markdown???

    由于 pandoc markdown在sublime下面并不能很好的预览,因此可以直接 使用 pandoc build成html,然后借助下面的工具进行实时在浏览器里面预览 http://www.br ...

  10. NSIP

    1. 第一章 信息安全概述 信息:信息是有意义的数据,具有一定的价值,是一种适当保护的资产,数据是是客观事务属性的记录,是信息的具体表现形式,数据经过加工处理之后 就是信息,而信息需要经过数字处理转换 ...