LibreOJ #109. 并查集

Posted ZlycerQan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于LibreOJ #109. 并查集的主要内容,如果未能解决你的问题,请参考以下文章

libreOJ模板并查集(输入挂,取模与find优化)

LOJ #109. 并查集

代码源 Div1 - 109#454. Minimum Or Spanning Tree(最小生成树,边权按位或,贪心,并查集) CF1624G

代码源 Div1 - 109#454. Minimum Or Spanning Tree(最小生成树,边权按位或,贪心,并查集) CF1624G

奶牛邻居——treap+契比雪夫距离+并查集

树的应用——并查集及实现代码