CF455C Civilization
嘟嘟嘟
水题一道,某谷又恶意评分。
合并无非是将两棵树的直径的中点连一块,记原来两棵树的直径为\(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的更多相关文章
- CF455C Civilization (并查集)
CF456E Codeforces Round #260 (Div. 1) C Codeforces Round #260 (Div. 2) E http://codeforces.com/conte ...
- cf455C Civilization (并查集)
并查集维护每个联通块的直径和最小的最大深度,每次连得时候连的肯定是最大深度最小的那两个点 #pragma GCC optimize(3) #include<bits/stdc++.h> # ...
- CF455C Civilization | luogu HXY造公园
题目链接: https://www.luogu.org/problemnew/show/P2195 http://codeforces.com/contest/455/problem/C 显然我们可以 ...
- CF455C Civilization 树的直径
问题描述 LG-CF455C 题解 首先,题目给出了 \(m\) 条边,对这 \(n\) 个点, \(m\) 条边组成的森林,跑出每棵树的直径,同时使用并查集维护树的连通性. 考虑合并两棵树的情况:设 ...
- NOIP前刷题记录
因为本蒻实在太蒻了...对于即将到来的NOIP2018ssfd,所以下决心要把自己近期做过的题目(衡量标准为洛谷蓝题难度或以上)整理一下,归归类,简单地写一下思路,就当作自己复习了吧qwq 本随笔持续 ...
- NOIP刷题
搜索 [NOIP2013]华容道 最短路+带剪枝的搜索,是一个思维难度比较大的题目. CF1064D Labyrinth 考虑贪心,用双向队列bfs [NOIP2017]宝藏 剪枝搜索出奇迹 题解:h ...
- Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...
- CodeForces 455C Civilization (并查集+树的直径)
Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a ...
- Codeforces Round #260 (Div. 1) C. Civilization 树的中心+并查集
题目链接: 题目 C. Civilization time limit per test1 second memory limit per test256 megabytes inputstandar ...
随机推荐
- Harbor 批量清理历史镜像
公司 Harbor 仓库一年多没清理,硬盘被堆满了,为此写了个批量清除的 Python 脚本. 源代码见 github-harbor_clean.py # coding: utf-8 from ope ...
- Arm-Linux 移植 alsa
ref : https://www.cnblogs.com/yutingliuyl/p/6718875.html https://blog.csdn.net/yuanxinfei920/article ...
- Django中ORM多对多表的操作
自己创建第三张表建立多对多关系 表的创建 # 老师表和学生表可以是一个多对多的关系,建表时可以手动建立第三张表建立关联 class Student(models.Model): name = mode ...
- linux配置sftp简单过程
首先疑惑是, 他需要的是上级的目录权限必须为root, 这点我有点不明白 环境是centos7.6 ssh 首先开整/etc/ssh/sshd_config # override default o ...
- win10 下的YOLO v3 的编译与使用
部署环境:win10 +CUDA 10.0 + vs2017 + opencv 3.4.0 代码版本是 https://github.com/AlexeyAB/darknet 1.初始准备 (1)下 ...
- 【SpringMVC】RESTful支持
一.概述 1.1 什么是RESTful 1.2 URL的RESTful实现 二.演示 2.1 需求 2.2 第一步更改DispatcherServlet配置 2.3 第二步参数通过url传递 2.4 ...
- 修改tomcat使用的的编码方式
默认情况下,tomcat使用的的编码方式:iso8859-1 修改tomcat下的conf/server.xml文件 找到如下代码: < Connector port="8080 ...
- Yum三方仓库——RPMForge
参考:How to Enable RPMForge Repository in RHEL/CentOS 7.x/6.x/5.x RPMForge / RepoForge这两个项目已经死亡,不应该使用 ...
- HTML中使用图像
插入图像 在页面中插入图像的标记只有一个,就是img标记. 语法为:<img src="图片地址" alt="下载失败时的替换文本" title='提示文 ...
- 【深度学习】Precision 和 Recall 评价指标理解
1. 四种情况 Precision精确率, Recall召回率,是二分类问题常用的评价指标.混淆矩阵如下: 预测结果为阳性 Positive 预测结果为假阳性 Negative 预测结果是真实的 Tr ...