CodeForces - 455C Civilization (dfs+并查集)
http://codeforces.com/problemset/problem/455/C
题意
n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出;2.将结点x和y所在的块连在一起,并使新块的最长路径最短。
分析
先想想最长路径怎么求,倘若我们以一个点为根,那么最长路径就是这棵有根树的直径了,那么我们可以先从任意点u出发,走到最远点v,再从v出发走到最远点,此时就能得出这棵树的直径了。现在想想怎么把两块合并?应该想到的是并查集,由于还得考虑合并后的直径最小,经过推理,由这两棵树的重心合并是最优的,也就是(d+1)/2的位置,由于两个重心连接会产生一条新边,所以+1。
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = 1e9+;
int T;
void testcase(){
printf("Case %d: ",++T);
}
const int MAXN = 3e5+;
const int MAXM = ; int n,m,Q,root,ans,tot,rec;
int fa[MAXN],num[MAXN],head[MAXN]; struct node{
int to,nxt;
}e[MAXN<<]; void addEdge(int u,int v){
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
} int Find(int x){
return fa[x]==x?x:fa[x]=Find(fa[x]);
}
void Union(int a,int b){
int xa=Find(a);
int xb=Find(b);
if(xa!=xb){
if(num[xa]<num[xb]) swap(xa,xb);
num[xa]=max(num[xa],(num[xa]+)/+(num[xb]+)/+);
fa[xb]=xa;
}
} void dfs(int u,int p,int d){
fa[u]=root;
if(d>ans){
ans=d;
rec=u;
}
for(int i=head[u];~i;i=e[i].nxt){
int v=e[i].to;
if(v!=p) dfs(v,u,d+);
}
} void init(){
tot=;
mset(head,-);
mset(num,);
for(int i=;i<=n;i++) fa[i]=i;
} int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int a,b;
scddd(n,m,Q);
init();
for(int i=;i<m;i++){
scdd(a,b);
addEdge(a,b);
addEdge(b,a);
}
for(int i=;i<=n;i++){
if(fa[i]==i){
root=rec=i;
ans=-;
dfs(i,,);
ans=-;
dfs(rec,,);
num[i]=ans;
}
}
while(Q--){
scd(a);
if(a==){
scd(b);
printf("%d\n",num[Find(b)]);
}else{
scdd(a,b);
Union(a,b);
}
}
return ;
}
CodeForces - 455C Civilization (dfs+并查集)的更多相关文章
- CodeForces 455C Civilization (并查集+树的直径)
Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a ...
- CodeForces 455C Civilization(并查集+树直径)
好久没有写过图论的东西了,居然双向边要开两倍空间都忘了,不过数组越界cf居然给我报MLE??这个题题意特别纠结,一开始一直不懂添加的边长是多长... 题意:给你一些点,然后给一些边,注意没有重边 环, ...
- Codeforces 455C Civilization(并查集+dfs)
题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...
- codeforces 456 E. Civilization(并查集+数的直径)
题目链接:http://codeforces.com/contest/456/problem/E 题意:给出N个点,M条边,组成无环图(树),给出Q个操作,操作有两种: 1 x,输出x所在的联通块的最 ...
- Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)
Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...
- Codeforces 1027D Mouse Hunt (强连通缩点 || DFS+并查集)
<题目链接> 题目大意: 有n个房间,每个房间都会有一只老鼠.处于第i个房间的老鼠可以逃窜到第ai个房间中.现在要清理掉所有的老鼠,而在第i个房间中防止老鼠夹的花费是ci,问你消灭掉所有老 ...
- CF455C Civilization (并查集)
CF456E Codeforces Round #260 (Div. 1) C Codeforces Round #260 (Div. 2) E http://codeforces.com/conte ...
- Codeforces Gym 100463E Spies 并查集
Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...
随机推荐
- 77 Linux commands and utilities you'll actually use
https://searchdatacenter.techtarget.com/tutorial/77-Linux-commands-and-utilities-youll-actually-use
- 设备 VMnet0 上的网桥当前未运行。此虚拟机无法与主机或网络中的其他计算机通信。
http://www.cnblogs.com/baihuitestsoftware/articles/4223552.html 因为试用Windows10教育版下的Docker打开过Hyper-V,虽 ...
- jQuery empty() vs remove()
https://stackoverflow.com/questions/3090662/jquery-empty-vs-remove http://www.cnblogs.com/yeer/archi ...
- Difference between prop and attr in different version of jquery
jQuery <1.9$('#inputId').attr('readonly', true); jQuery 1.9+$('#inputId').prop('readonly', true); ...
- SQLSERVER 2014 SP1 的服务器 日志文件无法收缩的处理
1. 公司一台服务器 日子会文件到了 100g+ 但是无法收缩 2. 根据同事的经验进行验证 dbcc loginfo 单独看改数据库的 dbcc loginfo("CWBASEGS60&q ...
- 软件工程_9th weeks
PSP DATE START_TIME END_TIME EVENT TYPE TIME 4.30-5.3 5:30 4:00 旅游 娱乐 72h 5.3 14:00 17:0 ...
- javaIO缓冲区
java中IO类分类. 图来自网络 缓冲区:应用程序在内存中开辟的一个空间.用来放置需要被写入或写出的数据. 使用缓冲区的 优点:使得应用程序操作磁盘(或者说是与磁盘的通信)的次数降低,提高应用程序的 ...
- python异常提示表
Python常见的异常提示及含义对照表如下: 异常名称 描述 BaseException 所有异常的基类 SystemExit 解释器请求退出 KeyboardInterrupt 用户中断执行(通常是 ...
- Spark_RDD之基本RDD操作
1.基本转化操作 1.1最常用的两个转化操作时map()和filter(). map()接收一个函数,把这个函数用于RDD中的每个元素,将函数作用之后的结果作为结果RDD中元素的值. filte ...
- VMware配置Linux虚拟机访问外网
[虚拟机版本] 系统版本 : Centos 6.8 [连接方法] 网络模式:桥接模式 ps:本人比较喜欢用桥接,直接NAT也是可以的 [配置步骤] 1.配置网卡 #配置命令 vi /etc/sysco ...