Codeforces455C. 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:

Andrew asks Dima about the length of the longest path in the region where city x lies.
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.

题意:求一棵里面的直径2333最长的最短路?第一次做 还有点小刺激呢,两棵树合并的时候,直径的处理贪心一下,毛估估应该是在直径的中点开始贪,那这课新树的直径就应该是 合并出来的那条路 (d1+1)/2+(d2+1)/2 +1和原来的两个直径里面取一个最大。最初的直径怎么求呢。由于是树没有环,那先走一次,走到底,找出那个最大的点,再反向求一次,取一个最大就可以了

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<vector>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<map>
using namespace std;

//thanks to pyf ...

#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
typedef pair<int,int> PII;
typedef long long ll;

const int N = 1e5+5;

struct Edge
{
    int u,v,next;
}edge[N*6];
int fa[N*3];
int head[N*3];
int val[N*3];
int tot=0;
int root;
int longest_p = 0;
int longest_l = 0;
int n;
void init()
{
    for(int i=0;i<=n;i++)
        fa[i] = i;
    CLR(head,-1);
    tot = 0;
    CLR(val,0);
}
void add_edge(int u,int v)
{
    edge[tot].u = u;
    edge[tot].v = v;
    edge[tot].next = head[u];
    head[u] = tot ++;
}
int find(int x)
{
    if(fa[x]!=x)
        fa[x] = find(fa[x]);
    return fa[x];   
}
void merge(int a,int b)
{
    a = find(a);
    b = find(b);
    if(a!=b)
    {
        fa[a] = fa[b];
        val[fa[b]] = max(max(val[a],val[b]),(val[a]+1)/2 + (val[b]+1)/2 + 1);
    }
}
void dfs(int u,int Fa,int d)
{
    fa[u] = root;
    if(d>longest_l)
    {
        longest_l = d;
        longest_p = u;
    }
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v = edge[i].v;
        if(v == Fa)
            continue;
        dfs(v,u,d+1);
    }
}
int main()
{
    int m,q;
    while(scanf("%d%d%d",&n,&m,&q)==3)
    {
        init();
        for(int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add_edge(u,v);
            add_edge(v,u);
        }
        for(int i=1;i<=n;i++)
        {
            if(fa[i]!=i)
                continue;
            root = i;
            longest_l = 0;
            longest_p = i;
            dfs(i,i,0);
    //      cout <<"***" <<  i << " " << longest_l << " " << longest_p << endl;
            val[root] = longest_l;
            dfs(longest_p,longest_p,0);
            val[root] = max(val[root],longest_l);   
        }
        for(int i=0;i<q;i++)
        {
            int op,x,y;
            scanf("%d",&op);
            if(op==1)
            {
                scanf("%d",&x);
                printf("%d\n",val[find(x)]);
            }
            else
            {
                scanf("%d%d",&x,&y);
    //          cout << "***" << find(x) << " " << find(y) << " " << val[find(x)] << " " << val[find(y)] << endl; 
                merge(x,y);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值