CodeForces 455C-Civilization

1 篇文章 0 订阅
1 篇文章 0 订阅

题目

C. Civilization
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 v1, v2, ..., vk, that there is a road between any contiguous cities vi and vi + 1 (1 ≤ i < k). The length of the described path equals to (k - 1). 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:

  1. Andrew asks Dima about the length of the longest path in the region where city x lies.
  2. 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·105; 0 ≤ m < n; 1 ≤ q ≤ 3·105) — 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, ai and bi (ai ≠ bi; 1 ≤ ai, bi ≤ n). These numbers represent the road between cities ai and bi. 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 xi. It is the request Andrew gives to Dima to find the length of the maximum path in the region that contains city xi (1 ≤ xi ≤ n).
  • 2 xi yi. It is the request Andrew gives to Dima to merge the region that contains city xi and the region that contains city yi (1 ≤ xi, yi ≤ n). Note, that xi can be equal to yi.
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

题意

给出一个森林,有一些询问,问森林里某棵树的直径,还有一些操作,把两棵树并成一棵,要求合并后树的直径最小。

解法

先求出所有树的直径,在通过并查集维护每个集合的最小直径,当两个集合合并时,最小直径应该是max(max(v[x],v[y]),(v[x]+1)/2+(v[y]+1)/2+1),x,y表示两个集合,v[]表示集合的最小值。

代码

#include<cstdio>
#include<cstring>
#define N 300005
struct edge
{
	int to,next;
}e[N*2];
int head[N],cnt,fa[N],v[N];
int mx,pos,vis[N];
int min(int a,int b)
{
	return a<b?a:b;
}
int max(int a,int b)
{
	return a>b?a:b;
}
void addedge(int u,int v)
{
	e[cnt].to=v;
	e[cnt].next=head[u];
	head[u]=cnt++;
}
int find(int x)
{
	if(fa[x]==x)
		return x;
	else
		return fa[x]=find(fa[x]);
}
void Union(int x,int y,int ty)
{
	int rx=find(x);
	int ry=find(y);
	if(rx==ry)
		return;
	fa[rx]=ry;
	if(ty)
		v[ry]=max(max(v[rx],v[ry]),(v[rx]+1)/2+(v[ry]+1)/2+1);
}
void dfs(int u,int pre,int ty,int len)
{
	int i;
	vis[u]=ty;
	if(len>mx)
	{
		mx=len;
		pos=u;
	}
	for(i=head[u];~i;i=e[i].next)
	{
		int v=e[i].to;
		if(v==pre)
			continue;
		dfs(v,u,ty,len+1);
	}
}
int main()
{
	int n,m,q,i,j,x,y,ty;
	scanf("%d%d%d",&n,&m,&q);
	for(i=1;i<=n;i++)
		fa[i]=i;
	memset(head,-1,sizeof(head));
	for(i=1;i<=m;i++)
	{
		scanf("%d%d",&x,&y);
		addedge(x,y);
		addedge(y,x);
		Union(x,y,0);
	}
	for(i=1;i<=n;i++)
	{
		if(vis[i])
			continue;
		mx=-1;
		dfs(i,-1,0,0);
		mx=-1;
		dfs(pos,-1,1,0);
		v[find(i)]=mx;
	}
	while(q--)
	{
		scanf("%d",&ty);
		if(ty==1)
		{
			scanf("%d",&x);
			printf("%d\n",v[find(x)]);
		}
		else
		{
			scanf("%d%d",&x,&y);
			Union(x,y,1);
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值