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 ...
随机推荐
- mysql 表注释的添加、查看 、修改
表创建时添加注释: create table user( id int not null default 0 comment '用户id', account varchar(20) not nul ...
- PDB自动启动以及Oracle Pfile的参数修改示范
1. Oracle12c 可以使用PDB的模式进行创建, 但是他一般不会自动启动,所以可以穿件一个触发器进行处理 创建语句 CREATE TRIGGER open_all_pdbs AFTER STA ...
- python 安装influxdb-python
一.Linux下安装 1.yum install -y git 2.安装pip,参考:https://app.yinxiang.com/shard/s41/sh/0338ba85-5443-453f- ...
- rabbitmq安装与使用总结
一.随着公司业务量的增加,原本部署在Windows服务器的RabbitMQ集群(3.6.1)总是出现莫名其妙的问题,经查询官方Issue,确认是RabbitMQ 3.6.1 版本的bug.查看从3.6 ...
- python之time模块:获取当前时间
time模块举例 import time # 获取当前时间戳 t = time.time() print('1)获取当前时间戳:', t) # 当前时间的struct_time形式 t = time. ...
- ansible系列2-常用命令
copyansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/ mode=0600 owner=oldboy group=oldboy &q ...
- 2015 HIAST Collegiate Programming Contest D
You have been out of Syria for a long time, and you recently decided to come back. You remember that ...
- day8 笔记
文件操作的最简单步骤open():打开文件,将句柄赋值给一个变量 read()write()等:操作文件 close():关闭文件,一定要关闭文件 ps:python会帮助你保存数据关闭文件,但是要在 ...
- 【ZOJ2277】The Gate to Freedom
BUPT2017 wintertraining(16) #4 E ZOJ - 2277 题意 输出\(n^n\)的首位的数字. 题解 用科学计数法表示\(n^n=k\cdot 10^b\),那么\(n ...
- Android 安装 卸载 更新 程序
安装程序的方法: .通过Intent机制,调出系统安装应用,重新安装应用的话,会保留原应用的数据. 1. String fileName =Environment.getExternalStorage ...