codeforces 455C Civilization

来源:互联网 发布:燕十八php教程百度云 编辑:程序博客网 时间:2024/05/24 06:49

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 62 1 22 3 42 5 62 3 22 5 31 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|1vector<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_JUDGEfreopen("in.txt","r",stdin);//  freopen("out.txt","w",stdout);#endifwhile(~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;}




    0 0