博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
阅读量:6119 次
发布时间:2019-06-21

本文共 4272 字,大约阅读时间需要 14 分钟。

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 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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-5const int inf=0x3f3f3f3f;const ll infll = 0x3f3f3f3f3f3f3f3fLL;inline ll read(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}//**************************************************************************************int N, M, Q, f[maxn], s[maxn];int root, ans, rec;vector
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] + 1) / 2 + (s[y] + 1) / 2 + 1);}void dfs (int u, int p, int d) { f[u] = root; if (d > ans) { ans = d; rec = u; } for (int i = 0; i < g[u].size(); i++) { if (g[u][i] != p) dfs(g[u][i], u, d+1); }}int main () { int type, u, v; scanf("%d%d%d", &N, &M, &Q); for (int i = 1; i <= N; i++) { f[i] = i; g[i].clear(); } for (int i = 0; i < M; i++) { scanf("%d%d", &u, &v); g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= N; i++) { if (f[i] == i) { root = rec = i; ans = -1; dfs(i, 0, 0); ans = -1; dfs(rec, 0, 0); s[i] = ans; } } for (int i = 0; i < Q; i++) { scanf("%d", &type); if (type == 1) { scanf("%d", &u); v = getfar(u); printf("%d\n", s[v]); } else { scanf("%d%d", &u, &v); link(u, v); } } return 0;}

 

代码:

转载地址:http://salka.baihongyu.com/

你可能感兴趣的文章
MS SQLSERVER通用存储过程分页
查看>>
60.使用Azure AI 自定义视觉服务实现物品识别Demo
查看>>
Oracle 冷备份
查看>>
jq漂亮实用的select,select选中后,显示对应内容
查看>>
C 函数sscanf()的用法
查看>>
python模块之hashlib: md5和sha算法
查看>>
linux系统安装的引导镜像制作流程分享
查看>>
解决ros建***能登录不能访问内网远程桌面的问题
查看>>
pfsense锁住自己
查看>>
vsftpd 相关总结
查看>>
bash complete -C command
查看>>
解决zabbix 3.0中1151端口不能运行问题
查看>>
售前工程师的成长---一个老员工的经验之谈
查看>>
Get到的优秀博客网址
查看>>
dubbo
查看>>
【Git入门之四】操作项目
查看>>
老男孩教育每日一题-第107天-简述你对***的理解,常见的有哪几种?
查看>>
Python学习--time
查看>>
在OSCHINA上的第一篇博文,以后好好学习吧
查看>>
高利率时代的结局,任重道远,前途叵测
查看>>