codeforces 455C 并查集
给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 并查集的更多相关文章
- Vladik and Entertaining Flags CodeForces - 811E (并查集,线段树)
用线段树维护每一块左右两侧的并查集, 同色合并时若不连通则连通块数-1, 否则不变 #include <iostream> #include <algorithm> #incl ...
- CodeForces - 893C-Rumor(并查集变式)
Vova promised himself that he would never play computer games... But recently Firestorm - a well-kno ...
- 0-1-Tree CodeForces - 1156D (并查集)
大意: 给定树, 边权为黑或白, 求所有有向路径条数, 满足每走过一条黑边后不会走白边. 这题比赛的时候想了个假算法, 还没发现..... 显然所求的路径要么全黑, 要么全白, 要么先全白后全黑, 所 ...
- Codeforces 980 并查集/模拟贪心最小字典序 找规律/数去除完全平方因子 逆思维倍增预处理祖先标记点
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...
- Codeforces 1166F 并查集 启发式合并
题意:给你一张无向图,无向图中每条边有颜色.有两种操作,一种是询问从x到y是否有双彩虹路,一种是在x到y之间添加一条颜色为z的边.双彩虹路是指:如果给这条路径的点编号,那么第i个点和第i - 1个点相 ...
- CodeForces - 1209D 并查集
题意: 有 n个不同的糖果,从 1到 n编号.有 k个客人.要用糖果招待客人.对于每个客人,这些糖果中恰有两个是其最爱.第 i个客人最爱的糖果编号是 xi和 y.将 k 个客人任意排列,他们按顺序去拿 ...
- Codeforces 722C(并查集 + 思维)
本文链接:http://www.cnblogs.com/Ash-ly/p/5932712.html 题目链接:http://codeforces.com/problemset/problem/722/ ...
- CodeForces 566D 并查集集合合并
#include <stdio.h> #include <algorithm> #define MAX 100000 #define LL long long #define ...
- Codeforces 455C Civilization(并查集+dfs)
题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...
随机推荐
- 《think in python》学习-3
函数 函数是指用于进行某种计算的一系列语句的有名称的组合. type(32) 就是一个函数调用的例子 类型转换函数 python提供了一些可将某个值从一种类型转换为另外一个类型的内置函数 int(32 ...
- .net通用权限框架B/S (三)--MODEL层(1)
1.新建c#类库 2.安装配置好entity frame5 3.新建的类库项目上右键"添加--新建项",选择AOD.NET实体数据模型 4.设置数据库连接, 5.选择建好的表 6. ...
- PowerDesigner使用方法小结
PowerDesigner多用来进行数据库模型设计,具有SQL语句自动生成等功能.当然,也有不少缺点,比如团队分享. 一.设置PowerDesigner模型视图中数据表显示列 1.Tools-Disp ...
- Editplus配置java运行环境
Editplus配置java运行环境 下载及安装: editplus官网下载地址:https://www.editplus.com/ 安装方法和安装普通exe应用程序一样,选在安装路径,下一步下一步, ...
- RandomAccessFile浅析
RandomAccessFile类中的write方法有以下的注意事项: 首先write方法每次都写入一个字节 api中write方法如下 public void write(int b) throws ...
- leetcode Linked List Cycle II python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- OC语法8——@class关键字
@class关键字: 在当前类中若要引用其他类的对象作成员变量(Book *book),我们以前采用的方式是 #import "Book.h" 但 #import "B ...
- threadid=1: thread exiting with uncaught exception (group=0x40db8930)
异常信息如下: 07-26 17:23:49.521: W/dalvikvm(29229): threadid=1: thread exiting with uncaught exception (g ...
- GoF——职责链模式
职责链模式(chain of Responsibility):使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它 ...
- OSG中的视角 eye up center
这三个值都是vec3变量,其中eye和center确定视角 eye就相当于人的眼睛,我们观察场景,是从这个坐标去看的,然后有了眼睛,我们观察得有一个方向,那么久需要另外一个坐标,就是c ...