传送门

给n个点, 初始有m条边, q个操作。

每个操作有两种, 1是询问点x所在的连通块内的最长路径, 就是树的直径。 2是将x, y所在的两个连通块连接起来,并且要合并之后的树的直径最小,如果属于同一个连通块就忽视这个操作。

先dfs出每个连通块的初始直径, 然后合并的话就是len[x] = max( (len[x]+1)/2+(len[y]+1)/2+1, max(len[x], len[y]));  然后搞一搞就好了。

一开始写bfs写挫了一直超时,  只好改成dfs......

 #include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 3e5+;
int f[maxn], num[maxn], head[maxn*], dis[maxn], cnt, q[maxn*];
struct node
{
int to, nextt;
}e[maxn*];
void add(int u, int v) {
e[cnt].to = v;
e[cnt].nextt = head[u];
head[u] = cnt++;
}
int findd(int u) {
return f[u] == u?u:f[u] = findd(f[u]);
}
void unionn(int x, int y) {
x = findd(x);
y = findd(y);
if(x == y)
return ;
f[y] = x;
int now = (+num[x])/+(num[y]+)/+;
num[x] = max(now, max(num[x], num[y]));
}
int maxx = , pos = -;
int dfs(int u, int fa, int d) {
if(d>maxx) {
maxx = d;
pos = u;
}
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == fa)
continue;
dfs(v, u, d+);
}
}
template<typename __ll>
inline void read(__ll &m)
{
__ll x=,f=;char ch=getchar();
while(!(ch>=''&&ch<='')){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
m=x*f;
}
int main()
{
int n, m, q, x, y;
cin>>n>>m>>q;
mem1(head);
for(int i = ; i<=n; i++) {
f[i] = i;
num[i] = ;
}
while(m--) {
read(x); read(y);
add(x, y);
add(y, x);
x = findd(x); y = findd(y);
f[x] = y;
}
for(int i = ; i<=n; i++) {
if(f[i] == i) {
dfs(i, -, );
maxx = ;
if(pos == -)
continue;
dfs(pos, -, );
num[i] = maxx;
maxx = ;
pos = -;
}
}
while(q--) {
int sign;
read(sign);
if(sign == ) {
read(x);
printf("%d\n", num[findd(x)]);
} else {
read(x); read(y);
unionn(x, y);
}
}
}

codeforces 455C 并查集的更多相关文章

  1. Vladik and Entertaining Flags CodeForces - 811E (并查集,线段树)

    用线段树维护每一块左右两侧的并查集, 同色合并时若不连通则连通块数-1, 否则不变 #include <iostream> #include <algorithm> #incl ...

  2. CodeForces - 893C-Rumor(并查集变式)

    Vova promised himself that he would never play computer games... But recently Firestorm - a well-kno ...

  3. 0-1-Tree CodeForces - 1156D (并查集)

    大意: 给定树, 边权为黑或白, 求所有有向路径条数, 满足每走过一条黑边后不会走白边. 这题比赛的时候想了个假算法, 还没发现..... 显然所求的路径要么全黑, 要么全白, 要么先全白后全黑, 所 ...

  4. Codeforces 980 并查集/模拟贪心最小字典序 找规律/数去除完全平方因子 逆思维倍增预处理祖先标记点

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...

  5. Codeforces 1166F 并查集 启发式合并

    题意:给你一张无向图,无向图中每条边有颜色.有两种操作,一种是询问从x到y是否有双彩虹路,一种是在x到y之间添加一条颜色为z的边.双彩虹路是指:如果给这条路径的点编号,那么第i个点和第i - 1个点相 ...

  6. CodeForces - 1209D 并查集

    题意: 有 n个不同的糖果,从 1到 n编号.有 k个客人.要用糖果招待客人.对于每个客人,这些糖果中恰有两个是其最爱.第 i个客人最爱的糖果编号是 xi和 y.将 k 个客人任意排列,他们按顺序去拿 ...

  7. Codeforces 722C(并查集 + 思维)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5932712.html 题目链接:http://codeforces.com/problemset/problem/722/ ...

  8. CodeForces 566D 并查集集合合并

    #include <stdio.h> #include <algorithm> #define MAX 100000 #define LL long long #define ...

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

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

随机推荐

  1. Net常用命名空间和类介绍

    一.基础命名空间 l  System.Collections 包含了一些与集合相关的类型,比如列表,队列,位数组,哈希表和字典等. l  System.IO 包含了一些数据流类型并提供了文件和目录同步 ...

  2. Python核心编程读笔 9: 异常

    第10章 异常一.异常1 检测和处理异常 (1)try-except语句 try: try_suite #监控这里的异常 except Exception[, reason]: except_suit ...

  3. 浅谈C中的指针和数组(二)

    原文转载地址:http://see.xidian.edu.cn/cpp/html/475.html 在原文的基础上增加自己的想法作为修改 很多初学者弄不清指针和数组到底有什么样的关系.我现在就告诉你: ...

  4. [转载]PDO防注入原理分析以及使用PDO的注意事项

    本文全部内容转载自月影无痕的博客http://zhangxugg-163-com.iteye.com/blog/1835721#bc2346092,感谢作者的分享 合理正确使用PDO,可以基本上防止S ...

  5. You have not agreed to the Xcode license.

    You have not agreed to the Xcode license. Before running the installer again please agree to the lic ...

  6. 关于QT、GCC、GNU下各个版本的下载地址

    http://download.qt.io/ http://ftp.gnu.org/gnu/gcc/ http://ftp.gnu.org/pub/gnu/

  7. EntityFramework、Dapper vs 草根框架性能

    EntityFramework.Dapper vs 草根框架性能 扯淡 当前市面上 ORM 很多,有跑车型的,如 Dapper,有中规中矩型的,如 Mybatis.Net,也有重量型的,如 Entit ...

  8. c++多线程编程之互斥对象(锁)的使用之----死锁

    一.死锁会在什么情况发生 1.假设有如下代码 mutex;   //代表一个全局互斥对象 void  A() { mutex.lock(); //这里操作共享数据 B();  //这里调用B方法 mu ...

  9. 项目代码摘抄,dot的用法之1

    function searchTags() { var list = $('#tags-list-select option:selected').val(); console.log(list); ...

  10. [置顶] How to compile openjdk 7 in RHEL5

    1. 为什么要编译openjdk的code? 因为从Eclipse调试JDK的代码时,方法中的局部变量不能显示,这样是因为编译JDK时,没有编译成debug版本. 2. RHEL5中自带的开发者JDK ...