树的直径 Codeforces Round #260 (Div. 2)E

来源:互联网 发布:濮阳市智慧网络 编辑:程序博客网 时间:2024/05/16 10:53

E. 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 citiesv1, v2, ..., vk, that there is a road between any contiguous citiesvi andvi + 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 cityx lies.
  2. Andrew asks Dima to merge the region where city x lies with the region where cityy 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 andbi (ai ≠ bi;1 ≤ ai, bi ≤ n). These numbers represent the road between citiesai andbi. 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 cityxi (1 ≤ xi ≤ n).
  • 2 xiyi. It is the request Andrew gives to Dima to merge the region that contains cityxi and the region that contains cityyi (1 ≤ xi, yi ≤ n). Note, thatxi can be equal toyi.
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

题意:有两种操作,一种是询问当前集合树的直径,另一个是合并两个集合。
对于第一种询问可以先预处理处来,两次dfs找出每个集合的直径(这个题用bfs找树的直径会超时,当然可能是自己写挫了),然后对于第二种操作,并查集合并,然后更新树的直径dis[x]=max(dis[x],(dis[x]+1)/2+(dis[y]+1)/2+1)(也就是上一篇博客中的三种情况,只不过这里边长度都是1,可以直接用不用再搜一遍了)

#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=300010;int n,m,q,maxid,maxdis;int pre[maxn],vis[maxn],dis[maxn];vector<int> g[maxn];void dfs(int u,int fa,int d,int r){    pre[u]=r;    if(d>maxdis)    {        maxdis=d;        maxid=u;    }    for(int i=0;i<g[u].size();i++)        if(g[u][i]!=fa)dfs(g[u][i],u,d+1,r);}int find(int x){    if(x==pre[x])return x;    return pre[x]=find(pre[x]);}void unite(int a,int b){    int x=find(a),y=find(b);    if(x==y)return ;    if(dis[x]<dis[y])swap(dis[x],dis[y]);    pre[y]=x;    int tmp=0;    dis[x]=max(dis[x],(dis[x]+1)/2+(dis[y]+1)/2+1);}int main(){    scanf("%d%d%d",&n,&m,&q);    for(int  i=0;i<m;i++)    {        int u,v;        scanf("%d%d",&u,&v);        g[u].push_back(v);        g[v].push_back(u);    }    for(int i=1;i<=n;i++)pre[i]=i;    memset(dis,0,sizeof(dis));    for(int i=1;i<=n;i++)        if(pre[i]==i)        {            maxdis=-1;            maxid=i;            dfs(i,0,0,i);            maxdis=-1;            dfs(maxid,0,0,i);            dis[find(i)]=maxdis;        }    while(q--)    {        int op,x,y;        scanf("%d%d",&op,&x);        if(op==1)printf("%d\n",dis[find(x)]);        else        {            scanf("%d",&y);            unite(x,y);        }    }    return 0;}





0 0
原创粉丝点击