cf455C Civilization (并查集)
并查集维护每个联通块的直径和最小的最大深度,每次连得时候连的肯定是最大深度最小的那两个点
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define pa pair<ll,ll>
#define CLR(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=3e5+; inline char gc(){
return getchar();
static const int maxs=<<;static char buf[maxs],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,maxs,stdin),p1==p2)?EOF:*p1++;
}
inline ll rd(){
ll x=;char c=gc();bool neg=;
while(c<''||c>''){if(c=='-') neg=;c=gc();}
while(c>=''&&c<='') x=(x<<)+(x<<)+c-'',c=gc();
return neg?(~x+):x;
} int N,M,Q;
int fa[maxn],low[maxn],len[maxn];
int up[maxn],dw[maxn];
int eg[maxn*][],egh[maxn],ect; inline void dfs1(int t,int x,int f){
fa[x]=t;
for(int i=egh[x];i;i=eg[i][]){
int b=eg[i][];if(b==f) continue;
dfs1(t,b,x);
dw[x]=max(dw[x],dw[b]+);
}
}
inline void dfs2(int t,int x,int f){
int ma=,se=;
low[t]=min(low[t],max(up[x],dw[x]));
for(int i=egh[x];i;i=eg[i][]){
int b=eg[i][];if(b==f) continue;
se=max(se,dw[b]+);
if(se>ma) swap(se,ma);
}
for(int i=egh[x];i;i=eg[i][]){
int b=eg[i][];if(b==f) continue;
if(dw[b]+!=ma) up[b]=max(up[x]+,ma);
else up[b]=max(up[x]+,se);
dfs2(t,b,x);
}
} inline pa dfs3(int x,int f,int d){
pa m=make_pair(d,x);
for(int i=egh[x];i;i=eg[i][]){
int b=eg[i][];if(b==f) continue;
m=max(m,dfs3(b,x,d+));
}return m;
} inline void adeg(int a,int b){
eg[++ect][]=b,eg[ect][]=egh[a],egh[a]=ect;
} inline int getf(int x){return x==fa[x]?x:fa[x]=getf(fa[x]);} int main(){
//freopen("","r",stdin);
int i,j,k;
N=rd(),M=rd(),Q=rd();
for(i=;i<=M;i++){
int a=rd(),b=rd();
adeg(a,b);adeg(b,a);
}
for(i=;i<=N;i++) fa[i]=i;
for(i=;i<=N;i++){
if(fa[i]==i){
low[i]=;
dfs1(i,i,);dfs2(i,i,);
len[i]=dfs3(dfs3(i,,).second,,).first;
}
}
for(i=;i<=Q;i++){
int o=rd();
if(o==) printf("%d\n",len[getf(rd())]);
else{
int a=rd(),b=rd();
a=getf(a),b=getf(b);
if(a==b) continue;
len[a]=max(max(len[a],len[b]),low[a]+low[b]+);
low[a]=min(max(low[a],low[b]+),max(low[b],low[a]+));
fa[b]=a;
}
}
return ;
}
cf455C Civilization (并查集)的更多相关文章
- 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(并查集+dfs)
题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...
- CF455C Civilization (并查集)
CF456E Codeforces Round #260 (Div. 1) C Codeforces Round #260 (Div. 2) E http://codeforces.com/conte ...
- CodeForces 455C Civilization (并查集+树的直径)
Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a ...
- CodeForces - 455C Civilization (dfs+并查集)
http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...
- CodeForces 455C Civilization(并查集+树直径)
好久没有写过图论的东西了,居然双向边要开两倍空间都忘了,不过数组越界cf居然给我报MLE??这个题题意特别纠结,一开始一直不懂添加的边长是多长... 题意:给你一些点,然后给一些边,注意没有重边 环, ...
- codeforces 456 E. Civilization(并查集+数的直径)
题目链接:http://codeforces.com/contest/456/problem/E 题意:给出N个点,M条边,组成无环图(树),给出Q个操作,操作有两种: 1 x,输出x所在的联通块的最 ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
随机推荐
- 弹性(flex)布局
五大主流浏览器及其内核:谷歌浏览器:Google Chrome.内核是blink火狐浏览器:Mozilla Firefox.内核是Gecko:欧鹏浏览器:OPera.内核是blink苹果浏览器:Saf ...
- react 路由 react-router-dom
import React from 'react'; import DataList from './data' import Tr from './Tr' // import One from '. ...
- java 工厂模式 转载
下面介绍三种设计模式,简单工厂模式,工厂方法模式,抽象工厂模式 思考如下场景: 有一天,林同学准备去买笔记本,他到商城发现有两款电脑他特别喜欢, 一款是 Macbook Pro, 另一款是 Surfa ...
- 【Java基础】求出1-100之间偶数和
结果:
- linux之ssh互信
1.如果你是之作root用户互信的话,直接一路执行如下命令就行. 1.切换你需要互信的账户 su hadoop1 2.执行命令,一路回车即可(在当前用的宿主用户目录下的.ssh目录下生成公钥和秘钥id ...
- jenkins 邮箱设置
一.先设置管理员邮箱地址 二.设置邮箱
- LeetCode & Online Programming Learning Platform
leetcode LeetCode is the best platform to help you enhance your skills, expand your knowledge and pr ...
- include与__autoload与命名空间namespace与PSR4详解
1. include, require, include_once, require_once include和require是PHP中引入源文件最基本的用法,其他例如__autoload, name ...
- DeepLearning网络设计总结
检测网络: 1. tiling层可以减少计算量,deconvolution相比tiling性能要好一些
- django--orm关系字段(ForeignKey、OneToOneField、ManyToManyField)详解
django中的关系字段 1.ForeignKey字段,即外键字段,对应一对多的情况,列如:一本书对应一个出版社,一个出版社可对应多本书. 2.ManyToManyFiled字段,即多对多字段,对应数 ...