蒟蒻86分求助!!!
查看原帖
蒟蒻86分求助!!!
455490
Sharpsmile楼主2021/9/12 19:50

rt,十四个点,十二个AC,一个WA,一个TLE

求大佬们帮忙看一下

先在这里给各位orz了

马疯 码风不是很好

//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <istream>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <string.h>
#include <map>
#define Inf 1e8;
using namespace std;
struct node {int di,val,ls,rs,fa;
    bool al;
}lt[100020];
int n,m;
int merge(int x,int y){
    if(x==0||y==0) return x|y;
    if(lt[x].val>lt[y].val) swap(x,y);
    lt[x].rs=merge(lt[x].rs,y);
    lt[lt[x].rs].fa=x;
    if(lt[lt[x].ls].di<lt[lt[x].rs].di){
        swap(lt[x].ls,lt[x].rs);
    }
    lt[x].di=lt[lt[x].rs].di+1;
    lt[lt[x].rs].fa=lt[lt[x].ls].fa=lt[x].fa=x;
    return x;
}
int findfa(int x){
    if(lt[x].fa==x) return x;
    else return findfa(lt[x].fa);
}

int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        lt[i].fa=i;
        cin>>lt[i].val;
        lt[i].di=1;
        lt[i].al=1;
    }
    for(int i=1;i<=m;i++){
        int d,x,y;
        cin>>d;
        if(d==1){
            cin>>x>>y;
            if(lt[x].al&&lt[y].al&&x!=y){
            x=findfa(x);
            y=findfa(y);
                merge(x,y);}
        }
        else {cin>>x;
            if(lt[x].al){
            x=findfa(x);
            cout<<lt[x].val<<endl;
            lt[x].al=0;
            x=merge(lt[x].ls,lt[x].rs);
            lt[x].fa=x;
            }
            else cout<<-1<<endl;
        }
    }
    return 0;
}
2021/9/12 19:50
加载中...