传送门

给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. How to close existing connections to a DB

    use master ALTER DATABASE YourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE --do you stuff here A ...

  2. oracle tablespace

    oracle tablespace 1. 查看所有表空间大小 SQL> select tablespace_name,sum(bytes)/1024/1024 from dba_data_fil ...

  3. Mysql中时间的操作笔记

    1.创建修改表时,为datetime字段设置当前时间为默认值 CREATE TABLE `NewTable` ( `id` int(11) NOT NULL AUTO_INCREMENT , `des ...

  4. jqeruy的append一个有趣的用法(挪移控件位置)

    使用:$(放置的容器).append($(需要迁移的控件)) 需求:列表中点击编辑可以在无刷修改 以前的方法是在列表中的每条数据直接放置控件.但是这个地区选择控件是服务器自定义控件不可以放置在列表的循 ...

  5. Android 动画系列

    Android种最常用的动画: ~1~Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转.平移.放缩和渐变) Tweene Animations 主要类: Animation   ...

  6. 移动端使用rem方法

    (function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? ...

  7. 在centos7下安装mysql5.7

    wget http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpmyum localinstall -y mysql57- ...

  8. 软件测试学习日志———— round 2 Junit+intellj idea 安装及简单的测试使用

    今天是软件测试的上机,主要内容是对junit的安装以及对一个简单类的测试实践.老师推荐用eclipse,但是我原来一直在 用intellj Idea,所以我试了试intellj Idea对junit的 ...

  9. c 函数传入数组。

    php 里面 直接 count($arr); 一个函数搞定, c里面想判断下数组的个数却非常困难. 想到php是C写的,那看看他的函数怎么写的不就行了... 哦,天啊,,事实比我想的要复杂的多... ...

  10. android 环境搭建 windows, linux

    android环境也搭建了很多次了,linux下window下.在这里记录下,以后再搭建设置变量啥的就直接看自己的博客就好了.电子挡笔记有时候也不方便 1.下载材料 概述:用的是比较简单的方式搭建环境 ...