题目链接

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

  • 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:
 /*************************************************************************
> File Name: E.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月09日 星期六 08时16分23秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int maxn = ;
int n, m, q;
int d[maxn], p[maxn], rank[maxn];
int x, w, best;
vector<int> g[maxn]; int getFa(int x) {
return x != p[x] ? p[x] = getFa(p[x]) : x;
} void dfs(int u, int fa, int high) {
p[u] = x;
if (high > best) best = high, w = u;
for (int i = ; i < (int)g[u].size(); i++) if (g[u][i] != fa)
dfs(g[u][i], u, high + );
} void unite(int x, int y) {
if (x == y) return ;
if (rank[x] >= rank[y]) {
p[y] = x;
d[x] = max(max(d[x], d[y]), (d[x]+)/+(d[y]+)/+);
if (rank[x] == rank[y]) rank[x]++;
} else {
p[x] = y;
d[y] = max(max(d[x], d[y]), (d[x]+)/+(d[y]+)/+);
}
} int main(void) {
scanf("%d %d %d", &n, &m, &q);
for (int i = ; i < m; i++) {
int from, to;
scanf("%d %d", &from, &to);
g[from].push_back(to);
g[to].push_back(from);
}
for (x = ; x <= n; x++) if (!p[x]) {
best = -; dfs(x, , );
best = -, dfs(w, , );
d[x] = best;
}
while (q--) {
int t;
scanf("%d %d", &t, &x);
if (t == ) {
printf("%d\n", d[getFa(x)]);
} else {
int y;
scanf("%d", &y);
unite(getFa(x), getFa(y));
}
}
return ;
}
 




												

Codeforces 455C的更多相关文章

  1. Codeforces 455C Civilization(并查集+dfs)

    题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...

  2. CodeForces - 455C Civilization (dfs+并查集)

    http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...

  3. Codeforces 455C Civilization:树的直径 + 并查集【合并树后直径最小】

    题目链接:http://codeforces.com/problemset/problem/455/C 题意: 给你一个森林,n个点,m条边. 然后有t个操作.共有两种操作: (1)1 x: 输出节点 ...

  4. CodeForces 455C Civilization (并查集+树的直径)

    Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a ...

  5. codeforces 455C 并查集

    传送门 给n个点, 初始有m条边, q个操作. 每个操作有两种, 1是询问点x所在的连通块内的最长路径, 就是树的直径. 2是将x, y所在的两个连通块连接起来,并且要合并之后的树的直径最小,如果属于 ...

  6. CodeForces 455C Civilization(并查集+树直径)

    好久没有写过图论的东西了,居然双向边要开两倍空间都忘了,不过数组越界cf居然给我报MLE??这个题题意特别纠结,一开始一直不懂添加的边长是多长... 题意:给你一些点,然后给一些边,注意没有重边 环, ...

  7. 暑期训练 CF套题

    CodeForces 327A 题意:有n个数,都是0或1,然后必须执行一次操作,翻转一个区间,里面的数0变1,1变0,求最多1的数量 思路:最开始我写的最大字段和,后面好像写搓了,然后我又改成暴力, ...

  8. Codeforces 划水

    Codeforces 566F 题目大意:给定$N$个数,任意两个数之间若存在一个数为另一个数的因数,那么这两个数存在边,求图中最大团. 分析:求一个图最大团为NP-Hard问题,一般不采用硬方法算. ...

  9. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

随机推荐

  1. java面向对象特征 — 一句话概括

    java基础学习总结之基本特征,最开始学习的时候,是形而上的理解,用了3年多,再回头看,理解起来颇为顺理成章 语言学习大概就是这样一种规律,学习,不甚解,应用,应用,渐深入人心,回头一看,恍然一悟 最 ...

  2. 【LGP5112】FZOUTSY

    题目 如果是\(hash\)做法的话显然就是把每一个位置后面的\(k\)个位置的hash值拿出来做一个莫队板子就好了 考虑一下牛逼的\(SAM\) 我们完全可以构造出来一棵后缀树,对于每个点找到其祖先 ...

  3. 表格Table好看的样式表

    caption { padding: 0 0 5px 0; width: 700px; font: italic 11px "Trebuchet MS" , Verdana, Ar ...

  4. 设置listContrl中指定行的颜色

    在MFC中 自己通过手动拖放CListCtrl控件来制作自己的表格: 目的: 将指定item的行更该颜色: 步骤: 1,在窗口中拖放CListCtrl控件, 单击右键 创建控件对象: CListCtr ...

  5. zip压缩工具类

    java将有关zip压缩的内容都封装在java.util.zip宝中,用java实现zip压缩,不用考虑压缩算法,java已经将这些进行了封装 实际上用java实现zip压缩涉及的就是一个“输入输出流 ...

  6. locate,find,df,mount,du命令

    1.locate找数据的时候,相当于去这个数据库里面查(locate查找的时候不扫描磁盘)查找图标文件:locate .icolocat -i 不区分大小写创建一个文件,该文件没有在数据库中,要想在数 ...

  7. 数据库顶会VLDB论文解读:阿里数据库智能参数优化的创新与实践

    前言 一年一度的数据库领域顶级会议VLDB 2019于美国当地时间8月26日-8月30日在洛杉矶召开.在本届大会上,阿里云数据库产品团队多篇论文入选Research Track和Industrial ...

  8. bzoj4574:Zjoi2016线段树 dp

    传送门 题解传送门 //Achen #include<algorithm> #include<iostream> #include<cstring> #includ ...

  9. mysql基础教程(一)-----概述、安装、查询

    概述 好处 •实现数据持久化 •使用完整的管理系统统一管理,易于查询 概念 DB 数据库(database):存储数据的“仓库”.它保存了一系列有组织的数据. DBMS 数据库管理系统(Databas ...

  10. pip安装requests报错unicodeEncodeError:'ascii' codec can\t encode charactesers in position 9-12:ordinal not in range(128)

    前提 : 已经安装pip(pip的安装我参考的是本博客转载脚本之家的步骤,实验可以成功) 1. 在cmd输入命令转到pip安装目录: 2. 运行后出现错误 3. 步骤2中的错误应该和编码有关.搜索百度 ...