玄学求调(玄关)
  • 板块灌水区
  • 楼主cqbzcjh
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/5/3 11:50
  • 上次更新2024/5/3 15:05:04
查看原帖
玄学求调(玄关)
906854
cqbzcjh楼主2024/5/3 11:50

P1196 [NOI2002] 银河英雄传说

#include<bits/stdc++.h>
using namespace std;
const int N=3e4+5;
int T,s[N],c[N],siz[N];
int find_set(int x){
	if(x!=s[x]){
		int t=find_set(s[x]);
		c[x]+=c[s[x]];
		s[x]=t;
	}
	return s[x];
}
void merge_set(int x,int y){
	x=find_set(x);y=find_set(y);
	if(x!=y){
		s[x]=y;
		c[x]=c[y]+siz[y];
		siz[y]+=siz[x];
	}
}
int main(){
	cin>>T;
	for(int i=1;i<=N;i++){
		s[i]=i;
		c[i]=0;
		siz[i]=1;
	}
	char op;
	int x,y;
	while(T--){
		cin>>op>>x>>y;
		if(op=='M'){
			merge_set(x,y);
			continue;
		}else{
			if(find_set(x)!=find_set(y)){
				printf("-1\n");
			}else{
				printf("%d\n",abs(c[x]-c[y])-1);
			}
		}
	}
	return 0;
}

c数组是当前节点到代表元的距离。 下载了洛谷第一个点的数据在本地测了是对的,但评测一个点都没过,实在不知道哪里错了,麻烦大佬帮忙看一下,谢谢!

2024/5/3 11:50
加载中...