输入输出-有一个写了加速会超时的题,得用scanf printf

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

#include<cstdio>
#include<iostream>
#define endl '\n'
#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.\n");
                    }
                    else  printf("In different gangs.\n"); 
                }
                else printf("Not sure yet.\n"); 
            }
            else{
                merge(a,b);
            }
        }
    }
    return 0;
}

 

posted @ 2020-03-13 22:11  SunCY  阅读(276)  评论(0编辑  收藏  举报