codeforces 455C Civilization

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 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 cities aiand 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
題意:有N個點,起初有M條邊,每條邊長度爲1,兩點之間沒有重邊。這樣構成了一個個連通塊。

有兩種操作。第一種是詢問某個點所在連通塊最長路徑長度。

第二種是連接兩個連通塊(可能屬於同一連通塊,這時不用操作),使得連接後的連通塊最長路徑最小。

思路:先預處理每個連通塊的最大長度,這可以用兩個dfs來解決,並用了並查集維護。

關於合併,易推得合併後最小的最大長度l3 = max(l1 , l2 , (l1+1)/2 + (l2+1)/2 + 1);

代碼:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <functional>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
typedef long long ll;
//#pragma comment(linker, "/STACK:1024000000,1024000000")  //手動擴棧
#define INF 1e9
#define maxn 310000
#define maxm 100086+10
#define mod 1000000007
#define eps 1e-7
#define PI acos(-1.0)
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define scan(n) scanf("%d",&n)
#define scanll(n) scanf("%I64d",&n)
#define scan2(n,m) scanf("%d%d",&n,&m)
#define scans(s) scanf("%s",s);
#define SZ(x) (int)G[x].size()
#define ini(a) memset(a,0,sizeof(a))
#define out(n) printf("%d\n",n)
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
vector<int> G[maxn];
int n,m,q;
int fa[maxn];
int findset(int x){ return fa[x] == x ? x : fa[x] = findset(fa[x]); }
bool vis1[maxn],vis2[maxn];
int len[maxn];
int mxid;
int mx;
void dfs1(int u,int dep)
{
	vis1[u] = 1;
	if(dep > mx ) { mx = dep; mxid = u;}
	for(int i = 0;i < SZ(u); i++)
	{
		int v = G[u][i];
		if(vis1[v]) continue;
		dfs1(v,dep + 1);
	}
}
void dfs2(int u,int dep)
{
	vis2[u] = 1;
	mx = max(mx,dep);
	for(int i = 0;i < (int)G[u].size(); i++)
	{
		int v = G[u][i];
		if(vis2[v]) continue;
		dfs2(v,dep+1);
	}
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	//  freopen("out.txt","w",stdout);
#endif
	while(~scanf("%d%d%d",&n,&m,&q))
	{
		rep1(i,n) G[i].clear();
		rep1(i,n) fa[i] = i;
		ini(vis1);
		ini(vis2);
		int a,b;
		rep(i,m)
		{
			scan2(a,b);
			G[a].push_back(b);
			G[b].push_back(a);
			int aa = findset(a);
			int bb = findset(b);
			if(aa != bb) fa[aa] = bb;
		}
		
		rep1(i,n)
		{
			int p = findset(i);
			if(!vis1[p])
			{
				mx = 0;
				dfs1(p, 0);
				mx = 0;
				dfs2(mxid,0);
				len[p] = mx;
			}
		}
		int ty;
		while(q--)
		{
			scan(ty);
			if(ty == 1)
			{
				scan(a);
				int p = findset(a);
				out(len[p]);
			}
			else
			{
				scan2(a,b);
				int p1 = findset(a);
				int p2 = findset(b);
				if(p1 == p2) continue;
				fa[p1] = p2;
				int t = max(len[p1],len[p2]);
				len[p2] = max(t,(len[p1]+1)/2 + (len[p2]+1)/2 + 1);
			}
		}
	}
	return 0;
}




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