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


    1.了解了各种输入挂性orz,找到了一个合适的 

    2.find用while写能快一倍,并且能被数据卡掉

    3.取模只能快十几毫秒,但也能被数据卡掉

    取模find双优化是1997mm过的 

    再加一个性价比较高的输入挂是438mm  23333

    #include <cstdio>
    #include <cmath>
    #include <complex>
    #include <algorithm>
    #include <iostream>
    #include<string.h>
    #include<vector>
    #include<ctime>
    #define rep(i,t,n)  for(int i =(t);i<=(n);++i)
    #define per(i,n,t)  for(int i =(n);i>=(t);--i)
    #define mmm(a,b) memset(a,b,sizeof(a))
    typedef long long ll;
    using namespace std;
    #define N 2333333
    const ll M= 998244353;
    const int maxn = 4e6 + 5;
    int f[maxn];
    using namespace std;
    
    template<typename T>inline void add_(T &A, int B, ll MOD = M) { A += B; (A >= MOD) && (A -= MOD); }
    template<typename T>inline void mul_(T &A, ll B, ll MOD = M) { A = (A*B) % MOD; }
    namespace IO
    {
        const int MAXL = 1 << 15;
        char buf[MAXL], *S, *T, ch;
    
        inline char Getch()
        {
            if (S == T) T = (S = buf) + fread(buf, 1, MAXL, stdin);
            return S == T ? EOF : *S++;
        }
    
        inline void Read(int &x)
        {
            x = 0;
            while (!isdigit(ch = Getch()));
            do { x = x * 10 + (ch ^ '0'); } while (isdigit(ch = Getch()));
        }
    }
    using namespace IO;
    
    int  find(int x) {
        while (f[x] ^ x)x = f[x] = f[f[x]]; return x;
    }
    void un(int x, int y) {
        int xx = find(x), yy = find(y);
        if(xx^yy)f[xx] = yy;
    }
    int main() {
        int n, m;
        cin >> n >> m;
        rep(i, 1, n)f[i] = i;
        int op, a,  b;
        ll ans = 0;
        rep(i, 1, m) {
            Read(op); Read(a); Read(b);
            if(!op)un(a, b);
            else {
                add_(ans, ans + (find(a) == find(b)));    
            }
        }
        cout << ans << endl;
        cin >> n;
    }
    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    python中如何对数据进行各种排序?
    js原型链
    js局部变量,参数
    计算字符串中每个字符出现次数
    推荐几个web中常用js图表插件
    getElementsByTagName("div")和$("div")区别
    Hadoop集群(第6期)JDK和SSH无密码配置
    Hadoop集群(第5期)SecureCRT使用
    Hadoop集群(第4期)VSFTP安装配置
    /etc/vsftpd/vsftpd.conf
  • 原文地址:https://www.cnblogs.com/SuuT/p/9648299.html
Copyright © 2020-2023  润新知