Codeforces 455C Civilization(并查集+dfs)
题目链接:Codeforces 455C Civilization
题目大意:给定N。M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的。然后是Q次操作。操作分为两种。一种是查询城市x所在的联通集合中。最长的路为多长。
二是连接两个联通集合,採用联通之后最长路最短的方案。
解题思路:由于一开时的图是不能够改变的,所以一開始用dfs处理出各个联通集合。而且记录住最大值。然后就是Q次操作,用并查集维护,注意由于联通的时候要採用最长路径最短的方案,所以s的转移方程变为s = max(s, (s+1)/2 + (s0+1)/2 + 1)
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 3 * 1e5 + 5;
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] + 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;
}
Codeforces 455C Civilization(并查集+dfs)的更多相关文章
- CodeForces - 455C Civilization (dfs+并查集)
http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...
- CodeForces 455C Civilization (并查集+树的直径)
Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a ...
- CodeForces 455C Civilization(并查集+树直径)
好久没有写过图论的东西了,居然双向边要开两倍空间都忘了,不过数组越界cf居然给我报MLE??这个题题意特别纠结,一开始一直不懂添加的边长是多长... 题意:给你一些点,然后给一些边,注意没有重边 环, ...
- Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...
- 牛客练习赛16 C 任意点【并查集/DFS/建图模型】
链接:https://www.nowcoder.com/acm/contest/84/C 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ...
- Codeforces 731C Socks 并查集
题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...
- codeforces 722C (并查集)
题目链接:http://codeforces.com/contest/722/problem/C 题意:每次破坏一个数,求每次操作后的最大连续子串和. 思路:并查集逆向操作 #include<b ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- 1021.Deepest Root (并查集+DFS树的深度)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
随机推荐
- [Apache手册]Linux环境下配置Apache运行cgi
CGI动态页面 相关模块 相关指令 mod_alias mod_cgi AddHandler Options ScriptAlias CGI(公共网关接口)定义了web服务器与外部内容生成程序之间交互 ...
- 【mysql】mysql中varcher属性最大值能存多长
1.首先理解varchar(n),n表示什么 MySQL5.0.3之前varchar(n)这里的n表示字节数 MySQL5.0.3之后varchar(n)这里的n表示字符数,比如varchar(200 ...
- UML类图几种关系的总结 【转】
在UML类图中,常见的有以下几种关系:泛化(Generalization), 实现(Realization),关联(Association),聚合(Aggregation),组合(Compositi ...
- metal &object c
https://developer.apple.com/documentation/metal/mtlcommandencoder/1458041-pushdebuggroup PushDebugGr ...
- C++11常用特性的使用经验总结(转载)
C++11已经出来很久了,网上也早有很多优秀的C++11新特性的总结文章,在编写本博客之前,博主在工作和学习中学到的关于C++11方面的知识,也得益于很多其他网友的总结.本博客文章是在学习的基础上,加 ...
- vmware虚拟机 C硬盘空间 无损扩容 新测
摘自: http://hi.baidu.com/y276827893/item/78a351f427726549932af214 其实上面一步的话, 虚拟机设置 里选择磁盘,实用工具里也有这个功能的. ...
- scrapy-splash抓取动态数据例子四
一.介绍 本例子用scrapy-splash抓取微众圈网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信息 ...
- 分享一个基于ligerui的系统应用案例ligerRM V2(权限管理系统)(提供下载)
阅读目录 简介 系统特色 系统介绍 - 首页 系统介绍 - 列表页 系统介绍 - 明细页(表单) 系统介绍 - 菜单/按钮 系统介绍 - 权限中心 系统介绍 - 数据权限 系统介绍 - 字段权限 系统 ...
- Spark2-对于Null/Nan的处理
一.几种查找空值的方法 1.Column方法 column.isNull/column.isNotNull/column.isNaN 2.类sql方法 二.na方法 2.1 na.drop方法 2.1 ...
- 【转载】Android控件属性大全
控件属性: android属性 Android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料, 第一类:属性值为true或falseandroid:layout_ ...