Codeforces 456E. Civilization (树的直径 + 并查集)

Description
Andrew plays a game called “Civilization”. Dima helps him.

The game has n cities and m bidirectional roads. The cities are numbered from 1 to n. Between any pair of cities there either is a single (unique) path, or there is no path at all. A path is such a sequence of distinct cities v 1 ,   v 2 ,   . . . ,   v k v_1, v_2, ..., v_k v1,v2,...,vk, that there is a road between any contiguous cities v i v_i vi and v i   +   1 v_{i + 1} vi+1 ( 1   ≤   i   <   k ) (1 ≤ i < k) (1i<k). The length of the described path equals to ( k   −   1 ) (k - 1) (k1). We assume that two cities lie in the same region if and only if, there is a path connecting these two cities.

During the game events of two types take place:

Andrew asks Dima about the length of the longest path in the region where city x lies.
Andrew asks Dima to merge the region where city x lies with the region where city y lies. If the cities lie in the same region, then no merging is needed. Otherwise, you need to merge the regions as follows: choose a city from the first region, a city from the second region and connect them by a road so as to minimize the length of the longest path in the resulting region. If there are multiple ways to do so, you are allowed to choose any of them.
Dima finds it hard to execute Andrew’s queries, so he asks you to help him. Help Dima.

Input
The first line contains three integers n , m , q ( 1   ≤   n   ≤   3 ⋅ 1 0 5 ; 0   ≤   m   <   n ; 1   ≤   q   ≤   3 ⋅ 1 0 5 ) n, m, q (1 ≤ n ≤ 3·10^5; 0 ≤ m < n; 1 ≤ q ≤ 3·10^5) n,m,q(1n3105;0m<n;1q3105) — the number of cities, the number of the roads we already have and the number of queries, correspondingly.

Each of the following m lines contains two integers, a i and b i ( a i ≠ b i; 1 ≤ a i, b i ≤ n). These numbers represent the road between cities a i and b i. There can be at most one road between two cities.

Each of the following q lines contains one of the two events in the following format:

1 x i. It is the request Andrew gives to Dima to find the length of the maximum path in the region that contains city x i (1 ≤ x i ≤ n).
2 x i y i. It is the request Andrew gives to Dima to merge the region that contains city x i and the region that contains city y i (1 ≤ x i, y i ≤ n). Note, that x i can be equal to y i.

Output
For each event of the first type print the answer on a separate line.

Examples
input
6 0 6
2 1 2
2 3 4
2 5 6
2 3 2
2 5 3
1 1
output
4

Solution
根据题目的描述,原图为森林。
对于森林中的每棵树,最远距离为其直径大小
对于树之间的合并操作,将两棵树的直径中点相连时,新树的直径最短
只需要再用并查集来维护所属关系,在合并时更新答案即可

Code

const int maxn = 3e5 + 5;


int n,m,q;
struct Edge{
	int v,next;
}e[maxn<<1];
int ecnt,head[maxn];
void add(int u,int v){
	e[++ecnt] = (Edge) {v,head[u]}, head[u] = ecnt;
	e[++ecnt] = (Edge) {u,head[v]}, head[v] = ecnt;
}

int fa[maxn], siz[maxn];
int find(int x){if(x == fa[x]) return x;return fa[x] = find(fa[x]);}

void unionset(int u,int v){
	int f1 = find(u), f2 = find(v);
	if(f1 == f2) return ;
	int nsiz = max(siz[f1], max(siz[f2], (siz[f1] + 1) / 2 + (siz[f2] + 1) / 2 + 1));
	if(siz[f1] < siz[f2]) {fa[f1] = f2; siz[f2] = nsiz;}
	else {fa[f2] = f1; siz[f1] = nsiz;}
	// fa[f1] = f2, siz[f2] = nsiz;
}

// bool vis[maxn];
int dis[maxn];

int bfs(int s){
	int t = s;
	queue<int>q;q.push(s);
	dis[s] = 0;
	while(!q.empty()){
		int u = q.front();q.pop();
		for(int i = head[u];i;i = e[i].next){
			int v = e[i].v;
			if(dis[v] || v == s) continue;
			dis[v] = dis[u] + 1;
			q.push(v);t = v;
		}
	}
	return t;
}

void clear(int u,int fa){
	dis[u] = 0;
	for(int i = head[u];i;i = e[i].next){
		int v = e[i].v;if(v == fa) continue;
		clear(v,u);
	}
}
void init(int n){
	for(int i = 1;i <= n;++i){
		int u = find(i);
		if(!dis[u]){
			int x = bfs(u);clear(u,u);
			int y = bfs(x);siz[u] = dis[y];
		}
	}
}
int main(){
	scanf("%d%d%d",&n,&m,&q);
	for(int i = 1;i <= n;++i) fa[i] = i;
	for(int i = 1,u,v;i <= m;++i){
		scanf("%d%d",&u,&v);add(u,v);unionset(u,v);
	}
	init(n);
	while(q--){
		int op,u,v;scanf("%d%d",&op,&u);
		if(op == 1) printf("%d\n", siz[find(u)]);
		if(op == 2) {scanf("%d",&v);unionset(u,v);}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值