C. Civilization

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/455/problem/C

Description

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.

Sample Input

6 0 6
2 1 2
2 3 4
2 5 6
2 3 2
2 5 3
1 1

Sample Output

4

HINT

题意

N个点m条边的无向图,有两种操作。

1.查询x块中的最长路是多少

2.连接x,y块,使得x,y中的最长路最小

题解:

并查集,用求树的直径的方法求这个块中的最长路

每次连接的时候,不需要真的连边,使直径小的指向直径大的,然后转移方程s[x]=max(s[x],(s[x]+1)/2+(s[y]+1)/2+1)

从每个直径的中点,连向另一个的中点

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 2000001
#define mod 10007
#define eps 1e-5
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** int N, M, Q, f[maxn], s[maxn];
int root, ans, rec;
vector<int> g[maxn]; int getfar(int x) {
return f[x] == x ? x : f[x] = getfar(f[x]);
} void link (int u, int v) {
int x = getfar(u);
int y = getfar(v); if (x == y)
return; if (s[x] < s[y])
swap(s[x], s[y]); f[y] = x;
s[x] = max(s[x], (s[x] + ) / + (s[y] + ) / + );
} void dfs (int u, int p, int d) {
f[u] = root; if (d > ans) {
ans = d;
rec = u;
} for (int i = ; i < g[u].size(); i++) {
if (g[u][i] != p)
dfs(g[u][i], u, d+);
}
} int main () {
int type, u, v; scanf("%d%d%d", &N, &M, &Q);
for (int i = ; i <= N; i++) {
f[i] = i;
g[i].clear();
} for (int i = ; i < M; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
} for (int i = ; i <= N; i++) {
if (f[i] == i) {
root = rec = i;
ans = -;
dfs(i, , ); ans = -;
dfs(rec, , );
s[i] = ans;
}
} for (int i = ; i < Q; i++) {
scanf("%d", &type);
if (type == ) {
scanf("%d", &u);
v = getfar(u);
printf("%d\n", s[v]);
} else {
scanf("%d%d", &u, &v);
link(u, v);
}
}
return ;
}

代码:

Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径的更多相关文章

  1. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

  2. Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)

    Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...

  3. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...

  4. Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心

    题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...

  5. Codeforces Round #260 (Div. 1) C. Civilization 树的中心+并查集

    题目链接: 题目 C. Civilization time limit per test1 second memory limit per test256 megabytes inputstandar ...

  6. Codeforces Round #541 (Div. 2)D(并查集(dsu),拓扑排序)

    #include<bits/stdc++.h>using namespace std;vector<int>g[2007];int fa[2007],vis[2007],num ...

  7. Codeforces Round #346 (Div. 2) E题 并查集找环

    E. New Reform Berland has n cities connected by m bidirectional roads. No road connects a city to it ...

  8. Codeforces Round #623 (Div. 2) D.Recommendations 并查集

    ABC实在是没什么好说的,但是D题真的太妙了,详细的说一下吧 首先思路是对于a相等的分类,假设有n个,则肯定要把n-1个都增加,因为a都是相等的,所以肯定是增加t小的分类,也就是说每次都能处理一个分类 ...

  9. Codeforces Round #812 (Div. 2) E(并查集)

    种类并查集:定义种类之间的关系来判断操作是否进行 题目大意:对于题目给出的一个矩阵,我们可以进行一种操作:swap(a[i][j],a[j][i]) 使得矩阵可以变换为字典序最小的矩阵 思路: 通过扫 ...

随机推荐

  1. 开源Math.NET基础数学类库使用(11)C#计算相关系数

    阅读目录 前言 1.Math.NET计算相关系数的类 2.Correlation的实现 3.使用案例 4.资源                本博客所有文章分类的总目录:[总目录]本博客博文总目录-实 ...

  2. 一个强大的LogParser的UI工具--logparserlizard简介

    日志分析,特别是IIS日志,一般人都会想到LogParser工具,的确很强.但是命令行的操作界面令很多非专业的管理人员望而生畏,现在好了,有一个可视化的LogParser的UI工具可以使用了! Log ...

  3. HTML5_画布_太阳系

    HTML5_画布_太阳系 一.canvas属性和方法的简单介绍①对于不支持canvas标签的浏览器需要显示"不支持canvas"使用IE11浏览器的开发人员工具,仿真:文档模式=8 ...

  4. 安装linux操作系统--浪潮服务器

    一直都是在虚拟机上进行安装linux操作系统,在服务器上安装的很少,也没有碰到过没找到驱动的情况,例如什么raid卡驱动,网卡驱动等异常情况的发生. 这次安装了两台服务器,浪潮的提供的服务器,硬盘是两 ...

  5. LRU Cache的实现

      代码如下:     来自为知笔记(Wiz)

  6. Linux中的.emacs文件

    刚开始的时候在Windows下使用emacs,那个时候配置 .emacs文件直接去C盘里\Users\(username)\AppData\Roaming 路径下查找就可以了(最开始的时候可以打开em ...

  7. IOS平台设计规范

    一.UI的控件概述: 1.框架UI的元素分为4类: A:栏:状态栏目和导航栏的结合体; B:内容视图:应用显示的内容信息,包括相关的交互行为,例如滚屏.插入.删除等操作进行排序; C:控制元素:产品行 ...

  8. MVC用户登录方法(lamda表达式)

        public bool ValidateUser(account model) { using (assertEntities db = new assertEntities()) { acc ...

  9. WS之cxf简单实现

    1.服务端实现: 1.1 定义接口,用@WebService修饰: /** @WebService 所修饰的接口,那么接口里面的方法全部都属于web的服务  */ @WebService public ...

  10. [转]软件开发过程(CMMI/RUP/XP/MSF)是与非?

    经常看到和听到大家在争论敏捷过程.RUP和CMM 哪个软件开发过程更好或者哪个过程不好,各自都有理由.争论得不亦乐乎......实际上,没有十全十美的过程,也不存在更好的过程.关键是什么样的过程适合自 ...