zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    多项式求逆
    luoguP3380 【模板】二逼平衡树(树套树)
    NTT
    poj2728 Desert King
    eclipse使用
    Java之面向对象
    Python实现终端高亮显示
    requests
    Go基础
    0919CSP-S模拟测试赛后总结
  • 原文地址:https://www.cnblogs.com/ZlycerQan/p/7076249.html
Copyright © 2011-2022 走看看