博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 455C
阅读量:7076 次
发布时间:2019-06-28

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

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·105; 0 ≤ 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 ≠ bi1 ≤ 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:

  • 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).
  • 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 6 2 1 2 2 3 4 2 5 6 2 3 2 2 5 3 1 1
output
4
思路:先计算出各个连通分量的最长的路径(两次dfs), 合并的时候,按秩合并,同时更新最长路。 Accepted Code:
1 /************************************************************************* 2     > File Name: E.cpp 3     > Author: Stomach_ache 4     > Mail: sudaweitong@gmail.com 5     > Created Time: 2014年08月09日 星期六 08时16分23秒 6     > Propose:  7  ************************************************************************/ 8  9 #include 
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 using namespace std;18 19 const int maxn = 300002;20 int n, m, q;21 int d[maxn], p[maxn], rank[maxn];22 int x, w, best;23 vector
g[maxn];24 25 int getFa(int x) {26 return x != p[x] ? p[x] = getFa(p[x]) : x;27 }28 29 void dfs(int u, int fa, int high) {30 p[u] = x;31 if (high > best) best = high, w = u;32 for (int i = 0; i < (int)g[u].size(); i++) if (g[u][i] != fa) 33 dfs(g[u][i], u, high + 1);34 }35 36 void unite(int x, int y) {37 if (x == y) return ;38 if (rank[x] >= rank[y]) {39 p[y] = x;40 d[x] = max(max(d[x], d[y]), (d[x]+1)/2+(d[y]+1)/2+1);41 if (rank[x] == rank[y]) rank[x]++;42 } else {43 p[x] = y;44 d[y] = max(max(d[x], d[y]), (d[x]+1)/2+(d[y]+1)/2+1);45 } 46 }47 48 int main(void) {49 scanf("%d %d %d", &n, &m, &q);50 for (int i = 0; i < m; i++) {51 int from, to;52 scanf("%d %d", &from, &to);53 g[from].push_back(to);54 g[to].push_back(from);55 }56 for (x = 1; x <= n; x++) if (!p[x]) {57 best = -1; dfs(x, 0, 0);58 best = -1, dfs(w, 0, 0);59 d[x] = best;60 }61 while (q--) {62 int t;63 scanf("%d %d", &t, &x);64 if (t == 1) {65 printf("%d\n", d[getFa(x)]);66 } else {67 int y;68 scanf("%d", &y);69 unite(getFa(x), getFa(y));70 }71 }72 return 0;73 }

 

 
 
 
 
 
 

转载于:https://www.cnblogs.com/Stomach-ache/p/3900808.html

你可能感兴趣的文章
两直线垂直的充要条件
查看>>
Review: Function Pointer
查看>>
*p=&a是把a的值赋给p,p=&a是把a的地址赋给p。
查看>>
input框限制只能输入正整数、字母、小数、汉字
查看>>
SQL Server 之登录
查看>>
21-Python与设计模式--备忘录模式
查看>>
JavaScript学习(1)之JavaScript基础
查看>>
用verilog模拟DDS产生正弦波信号
查看>>
Spring中如何使用设计模式
查看>>
聊聊Dubbo(九):核心源码-服务端启动流程2
查看>>
BZOJ 4589 Hard Nim
查看>>
从源码分析如何优雅的使用 Kafka 生产者
查看>>
js实现touch移动触屏滑动事件
查看>>
XAMPP PHPSTORM XDEBUG 配合使用
查看>>
51 nod 1681 公共祖先 (主席树+dfs序)
查看>>
jquery easy ui
查看>>
mysql编程--创建函数出错的解决方案
查看>>
递归案例:汉诺塔问题
查看>>
Lucene 个人领悟 (二)
查看>>
JSVC技术
查看>>