传送门

给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. javascript高级知识点——函数的长度

    代码信息来自于http://ejohn.org/apps/learn/. 函数的长度属性如何工作? function makeNinja(name){} function makeSamurai(na ...

  2. OCP prepare 20140628

    1. null if       nvl     nvl2 NULLIF函数 Oracle NULLIF函数语法为NULLIF(表达式1,表达式2),如果表达式1和表达式2相等则返回空值,如果表达式1 ...

  3. leetcode Valid Parentheses python

    # 解题思路: # 创建一个字典映射关系 dicts# 使用一个栈stk 遍历字符串s 得到一个新的字符串curItem 如果lastItem在dicts中的value和它相等 不做任何操作# 如果不 ...

  4. JVM学习之强引用、弱引用、软引用、虚引用

    转自:http://my.oschina.net/ydsakyclguozi/blog/404389 多谢博主分享 1.强引用(StrongReference) 强引用是使用最普遍的引用.如果一个对象 ...

  5. PHP基础示例:商品信息管理系统v1.1

    实现目标:使用php和mysql写一个商品信息管理系统,并带有购物车功能 一.创建数据库和表 1.创建数据库和表:demodb 2.创建表格:goods 字段:商品编号,商品名称,商品类型,商品图片, ...

  6. Android R.layout. 找不到已存在的布局文件

    今天写新页面的时候,突然发现R.layout.  无法找到我已经写好的页面,于是顿时就不淡定了. 把R文件翻了一遍  发现也没有.... 然后我就看到了这个. android.R 原来是我错把Andr ...

  7. artdialog关闭弹出窗口

    打开 function opentree(){  var dialog = art.dialog({    title: '选择提交部门',      content:jQuery("#my ...

  8. GoF——组合模式

    组合模式:将对象组合成树形结构以表示“部分-真题”的结构层次.组合模式使得用户对单个对象和组合对象的使用具有一致性. 结构图: using System; using System.Collectio ...

  9. SilverLight搭建WCF聊天室详细过程

    收藏SL双工通信例子教程 SilverLight 4正式版发布给开发人员带来了更多功能,并且4已经支持NET.TCP协议,配合WCF开发高效率的交互应用程序已经不再是难事,本系列文章主要针对已经完成的 ...

  10. Built-in Functions学习

    abs(x) :返回一个数字的绝对值,可以是整形也可以是浮点型. all(iterable):返回True,如果所以迭代对象的元素为true,或者可迭代对象为空. any(iterable):如果可迭 ...