# LOJ #109. 并查集

1

#### 题目描述

• 加入一条连接 uuu 和 vvv 的无向边
• 查询 uuu 和 vvv 的连通性

#### 输入格式

• 如果 op=0\text{op} = 0op=0，则表示加入一条连接 uuu 和 vvv 的无向边；
• 如果 op=1\text{op} = 1op=1，则表示查询 uuu 和 vvv 的连通性。

#### 样例输入

3 6
1 1 0
0 0 1
1 0 1
1 1 2
0 2 1
1 2 1

#### 样例输出

5

#### 数据范围与提示

n≤4000000,m≤8000000n\le 4000000,m\le 8000000n≤4000000,m≤8000000

#### 显示分类标签

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cmath>
5 using namespace std;
6 const int MAXN=8000001;
7 const int mod=998244353;
9 {
10     char c=‘+‘;bool flag=0;n=0;
11     while(c<‘0‘||c>‘9‘) c==‘-‘?flag=1,c=getchar():c=getchar();
12     while(c>=‘0‘&&c<=‘9‘) n=n*10+c-48,c=getchar();
13 }
14 int fa[MAXN];
15 int size[MAXN];
16 int n,m;
17 string p;
18 int find(int x)
19 {return fa[x]==x?fa[x]:fa[x]=find(fa[x]);}
20 int query(int x,int y)
21 {return find(x)==find(y);}
22 void unionn(int x,int y)
23 {
24     int fx=find(x);int fy=find(y);
25     if(fx!=fy)
26     {
27         if(size[fx]>size[fy])    swap(fx,fy);
28         fa[fx]=fy;    size[fy]+=size[fx];
29         //fa[fx]=fy;
30     }
31 }
32 int ans=0;
33 int main()
34 {
35     //freopen("a.in","r",stdin);
36     //freopen("a.out","w",stdout);
38     for(int i=1;i<=n;i++)    fa[i]=i;
39     for(int i=1;i<=m;i++)
40     {
42         if(how)// 询问
43         {
45             ans=(ans*2+query(x,y))%mod;
46         }
47         else//连边
48         {
50             unionn(x,y);
51         }
52     }
53     printf("%d",ans);
54     return 0;
55 }

## (线段树+并查集) Codeforces Round #416 (Div. 2) E Vladik and Entertaining Flags

In his spare time Vladik estimates beauty of the flags. Every flag could be represented as the matrix n?×?m which consists of positive integers. Let's define the beauty of the flag as number of components in its matrix. We call component a set of cel

## poj 2513 并查集，Trie（字典树）， 欧拉路径

- Colored Sticks POJ - 2513 You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

## ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集

Untrusted Patrol Time Limit: 3 Seconds                                     Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure t

## 并查集详解 (转)

http://blog.csdn.net/dellaserss/article/details/7724401 我从CSDN转的文章,原文作者我也不懂是谁,文章写得真的是诙谐幽默,使得内容更容易理解了. 来看一个实例,杭电OJ 1232畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可).问最少还需要建

## HDU 1811：Rank of Tetris（并查集+拓扑排序）

http://acm.hdu.edu.cn/showproblem.php?pid=1811 Rank of Tetris Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球.为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜,定时更新,名堂要比福布斯富豪榜还响.关于如何排名,这个不用说都知道是根据Rating从高到低来排,如果两个人具有相同的Rating,那就按