A9661 | Civilization
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $v_{1},v_{2},...,v_{k}$ , that there is a road between any contiguous cities $v_{i}$ and $v_{i+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.
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 $v_{1},v_{2},...,v_{k}$ , that there is a road between any contiguous cities $v_{i}$ and $v_{i+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.
输入格式
The first line contains three integers $n$ , $m$ , $q$ ( $1<=n<=3·10^{5}$ ; $0<=m<n$ ; $1<=q<=3·10^{5}$ ) — 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, $a_{i}$ and $b_{i}$ ( $a_{i}≠b_{i};$ $1<=a_{i},b_{i}<=n$ ). These numbers represent the road between cities $a_{i}$ and $b_{i}$ . 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$ $x_{i}$ . It is the request Andrew gives to Dima to find the length of the maximum path in the region that contains city $x_{i}$ ( $1<=x_{i}<=n$ ).
- $2$ $x_{i}$ $y_{i}$ . It is the request Andrew gives to Dima to merge the region that contains city $x_{i}$ and the region that contains city $y_{i}$ ( $1<=x_{i},y_{i}<=n$ ). Note, that $x_{i}$ can be equal to $y_{i}$ .
Each of the following $m$ lines contains two integers, $a_{i}$ and $b_{i}$ ( $a_{i}≠b_{i};$ $1<=a_{i},b_{i}<=n$ ). These numbers represent the road between cities $a_{i}$ and $b_{i}$ . 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$ $x_{i}$ . It is the request Andrew gives to Dima to find the length of the maximum path in the region that contains city $x_{i}$ ( $1<=x_{i}<=n$ ).
- $2$ $x_{i}$ $y_{i}$ . It is the request Andrew gives to Dima to merge the region that contains city $x_{i}$ and the region that contains city $y_{i}$ ( $1<=x_{i},y_{i}<=n$ ). Note, that $x_{i}$ can be equal to $y_{i}$ .
输出格式
For each event of the first type print the answer on a separate line.
输入输出样例
输入 #1
6 0 6 2 1 2 2 3 4 2 5 6 2 3 2 2 5 3 1 1
输出 #1
4
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted