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;
}

 

posted @ 2017-06-25 10:27  ZlycerQan  阅读(184)  评论(0编辑  收藏  举报