CF 455C--Civilization

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 nmq (1 ≤ n ≤ 3·1050 ≤ m < n1 ≤ 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 citiesai 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.

Sample test(s)
input
6 0 6
2 1 2
2 3 4
2 5 6
2 3 2
2 5 3
1 1
output
4
思路:先每個聯通快找最長路。然後合併三者取最大。兩個集合的max和各自一半相連三個取個大的。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cmath>

using namespace std;
#define inf 0x3f3f3f3f
#define maxn 300080
#define maxm 600170
#define LL long long int
int father[maxn],len[maxn];
bool vis[maxn];
int first[maxn],vv[maxm],nxt[maxm],dis[maxm];
int n,m,q;
int find(int x)
{
	if(x == father[x])	return x;
	return father[x] = find(father[x]);
}
void Union(int u,int v)
{
	int fu = find(u),fv = find(v);
	if(fu == fv)	return;
	father[fu] = fv;
	int a = len[fu],b = len[fv];
	int c = max(a,b);
	c = max(c,len[fu]/2+1+len[fv]/2+1);
	len[fv] = c;
}
int e,maxdis,pos;
void init()
{
	e  = 0;
	memset(first,-1,sizeof(first));
	memset(vis,0,sizeof(vis));
}
void addedge(int u,int v)
{
	vv[e] = v;	nxt[e] = first[u]; first[u] = e++;
	vv[e] = u;	nxt[e] = first[v]; first[v] = e++;
}
void dfs(int u,int pre)
{
	vis[u] = 1;
	for(int i = first[u];i != -1;i = nxt[i])
	{
		int v = vv[i];
		if(v == pre)	continue;
		dis[v] = dis[u] +1;
		if(dis[v] > maxdis)
		{
			maxdis = dis[v];
			pos = v;
		}
		dfs(v,u);
	}
}
void dfs1(int u,int pre)
{
	for(int i = first[u];i != -1;i = nxt[i])
	{
		int v = vv[i];
		if(v == pre)	continue;
		dis[v] = dis[u] +1;
		if(dis[v] > maxdis)
		{
			maxdis = dis[v];
			pos = v;
		}
		dfs(v,u);
	}
}
void dfs2(int u,int pre,int f)
{
	for(int i = first[u];i != -1;i = nxt[i])
	{
		int v = vv[i];
		if(v == pre)continue;
		father[v] = f;
		dfs2(v,u,f);
	}
}
int main()
{
//	freopen("in.txt","r",stdin);
	while(scanf("%d%d%d",&n,&m,&q)==3)
	{
		for(int i = 1;i <= n;i++)	
		{
			first[i] = -1;
			father[i] = i;len[i] = 1;
		}
		for(int i = 1;i <= m;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			addedge(u,v);
		}
		memset(vis,0,sizeof(vis));
		for(int i = 1;i <= n;i++)
		{
			pos = i,maxdis = 0;
			if(!vis[i])	
			{
				dis[i] = 0;
				dfs(i,-1);
				dis[pos] = 0;
				dfs1(pos,-1);
				dfs2(pos,-1,pos);
				len[pos] = maxdis+1;
			}
			
		}
		for(int i = 1;i <= q;i++)
		{
			int ope,x,y;
			scanf("%d",&ope);
			if(ope == 1)
			{
				scanf("%d",&x);
				printf("%d\n",len[find(x)]-1);
			}
			else 
			{
				scanf("%d%d",&x,&y);
				if(x == y)	continue;
				Union(x,y);
			}
		}
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章