zoukankan      html  css  js  c++  java
  • 【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;
    }
    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    mysql怎么导入大文件的sql文件
    php函数研究
    php实现实现代码多主从,切换,轮询,健康检查
    php实现单个用户禁止重复登录,防止同一用户同时登陆
    php使用p3p实现cookies跨域设置 实现单点登录,全站登录
    实现页面浏览统计
    遍历目录删除指定MD5值的文件
    boot.img的修改
    “逃离大厦”游戏的破解
    Android漏洞——将Android恶意代码隐藏在图片中
  • 原文地址:https://www.cnblogs.com/SuuT/p/9648299.html
Copyright © 2011-2022 走看看