大哥帮帮看看代码把,孩子都对比题解好多遍了也没找到问题哇
查看原帖
大哥帮帮看看代码把,孩子都对比题解好多遍了也没找到问题哇
845927
wendadawen楼主2022/12/20 23:10
#include <iostream>
using namespace std;
const int N = 1e5+7;
const int inf = INT_MAX;
int rc[N],lc[N],dis[N],fa[N],val[N];
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int merge(int x,int y) {
    if(!x || !y) return x+y;
    if(val[x] > val[y] || (val[x]==val[y] && x>y)) swap(x,y);
    rc[x] = merge(rc[x],y);
    if(dis[lc[x]] < dis[rc[x]]) swap(lc[x],rc[x]);
    dis[x] = dis[rc[x]] + 1;
    return x;
}
int main() {
    int n,m;
    cin >> n >> m;
    for(int i=1;i<=n;++i) {cin>>val[i];fa[i]=i;}
    dis[0] = -1;
    int op,x,y;
    while(m--){
        cin >> op;
        if(op==1) {
            cin >> x >> y;
            x = find(x); y = find(y);
            if(val[x]==-inf || val[y]==-inf || x==y) continue;
            fa[x] = fa[y] = merge(x,y);
        } else {
            cin >> x;
            if(val[x] == -inf) {cout << -1 << endl; continue;}
            x = find(x);
            cout << val[x] << endl;
            fa[lc[x]] = fa[rc[x]] = fa[x] = merge(lc[x],rc[x]);
            lc[x] = rc[x] = dis[x] = 0; val[x] = -inf;   
        }
    }
    system("pause");
    return 0;
}


2022/12/20 23:10
加载中...