LibreOJ #109. 并查集

二次联通门 : LibreOJ #109. 并查集

/*
    LibreOJ #109. 并查集

    并查集
*/
#include <cstdio>

#define Max 4000090
#define Mod 998244353

void read (int &now)
{
    now = 0;
    register char word = getchar ();
    while (word < ‘0‘ || word > ‘9‘)
        word = getchar ();
    while (word >= ‘0‘ && word <= ‘9‘)
    {
        now = now * 10 + word - ‘0‘;
        word = getchar ();
    }
}

int N, M;
int father[Max];

int Find (int x)
{
    return father[x] == x ? x : father[x] = Find (father[x]);
}

inline void Merge (int x, int y)
{
    int now_1 = Find (x);
    int now_2 = Find (y);

    if (x != y)
        father[x] = y;
    return ;
}

inline int Query (int x, int y)
{
    return Find (x) == Find (y);
}

int number[Max];

int main (int argc, char *argv[])
{
    read (N);
    read (M);

    int x, y, type;
    register int cur = 0;
    for (register int i = 1; i <= N; i ++)
        father[i] = i;

    long long Answer = 0;
    for (; M --; )
    {
        read (type);
        read (x);
        read (y);
        x ++;
        y ++;
        if (type == 0)
            Merge (x, y);
        else
            Answer = ((Answer << 1) + Query (x, y)) % Mod;
    }

    printf ("%lld", Answer);
    return 0;
}
时间: 2024-10-23 08:50:19

LibreOJ #109. 并查集的相关文章

LOJ #109. 并查集

内存限制:256 MiB时间限制:2000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论 1 测试数据 题目描述 这是一道模板题. 维护一个 nnn 点的无向图,支持: 加入一条连接 uuu 和 vvv 的无向边 查询 uuu 和 vvv 的连通性 由于本题数据较大,因此输出的时候采用特殊的输出方式:用 000 或 111 代表每个询问的答案,将每个询问的答案一次从左到右排列,把得到的串视为一个二进制数,输出这个二进制数 mod 998244353\t

并查集应用

题目描述: One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls

(线段树+并查集) 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?

UVA 572 油田连通块-并查集解决

题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... 1 #include <cstdio> 2 #include <iostream> 3 #include <sstream> 4 #include <cmath> 5 #include <cstring> 6 #include <cstdlib&

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

【CF766D】Mahmoud and a Dictionary(并查集)

题意:有n个单词,给定m个关系,每个关系要么表示单词a与单词b相同,要么表示单词a与单词b相反. 并且"相同"与"相反"有性质:若a与b相同,b与c相同,则a与c相同(从而单词的相同关系是等价关系): 若a与b相反,b与c相反,则a与c相同.按顺序判断这m个关系是否可以成立,若可以成立,则加上这个关系,否则忽略. 再给定q个询问,每个询问 查询单词a与单词b的关系(相同.相反或未知). n,m,q<=10^5 思路:并查集 设与i相反的单词集合中的代表为fan

并查集详解 (转)

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,那就按