zoukankan      html  css  js  c++  java
  • 输入输出-有一个写了加速会超时的题,得用scanf printf

       带标记的并查集  ,原来加速后也不见得就很快了

    #include<cstdio>
    #include<iostream>
    #define endl '
    '
    #define _for(i,a,b) for(int i=a;i<b;i++)
    using namespace std;
    const int N = 1e5+5;
    typedef long long ll;
    struct Node{
        int p,r;
    }o[N];
    int n,m;
    int find(int k){
        if( k == o[k].p ) return k;
        
        int t = o[k].p;
        o[k].p = find( o[k].p );
        o[k].r = ( o[k].r+o[t].r )%2;
        return o[k].p;
    }
    void merge(int a,int b){
        int A = find(a),B = find(b);
        if( A == B ) return ;
        o[A].p = B;
        o[A].r = (o[a].r+1+o[b].r)%2;
        
    }
    int main(){ 
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); 
        int t; scanf("%d",&t);
        while(t--){
    //        cin>>n>>m;
            scanf("%d %d",&n,&m);
            _for(i,1,n+1) o[i].p = i,o[i].r = 0;
            while(m--){
                char s[3]; int a,b;
    //            cin>>s>>a>>b;
                scanf("%s %d %d",s,&a,&b); 
                if(s[0]=='A'){
                    int A = find(a),B = find(b);
    //                cout<<a<<" "<<b;
                    if(A==B){
                        if( o[a].r==o[b].r ){
    //                        cout<<"In the same gang."<<endl;
                            printf("In the same gang.
    ");
                        }
                        else  printf("In different gangs.
    "); 
                    }
                    else printf("Not sure yet.
    "); 
                }
                else{
                    merge(a,b);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    【树论】新二叉树
    【贪心】umi的函数
    【模拟】高精度练习之加法
    【模拟】高精度练习之减法
    【贪心】线段覆盖
    【树论】二叉树的序遍历
    【贪心】超级书架
    【动态规划】爱与愁的心痛
    【图论】牛大赛
    【图论】产生数
  • 原文地址:https://www.cnblogs.com/SunChuangYu/p/12489434.html
Copyright © 2011-2022 走看看