求助:样例输到一半输出两个-1后程序突然退出
查看原帖
求助:样例输到一半输出两个-1后程序突然退出
448884
快乐的大童楼主2021/7/18 20:01
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<cctype>
#include<vector>
#include<utility>
#include<queue>
#include<map>
#include<set>
#include<ctime>
#include<deque>
#include<fstream>
#include<cstdlib>
#define f(a,b) for(int a=1;a<=b;a++)
#define F(a,b,c) for(int a=b;a<=c;a++)
using namespace std;
const int N=30001;
const int inf=999999999;
const int mod=1000000007;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int father[N],line[N],frt[N],t;
int getf(int x){
	if(father[x]==x) return x;
	int k=getf(father[x]);
	frt[x]+=frt[father[x]];
	return father[x]=k;
}
int main(){
	std::ios::sync_with_stdio(false);
	f(i,N){
		father[i]=i;
		frt[i]=0;
		line[i]=1;
	}
	t=read();
	f(s,t){
		char ch=getchar();
		getchar();
		int x,y;
		cin>>x>>y;
		int p=getf(x),q=getf(y);
		if(ch=='M'){
			father[p]=q;
			frt[p]+=line[q];
			line[q]+=line[p];
			line[p]=0;
		}else{
			if(p!=q) cout<<-1<<endl;
			else cout<<abs(frt[p]-frt[q])-1<<endl;
		}
	}
	return 0;
}

2021/7/18 20:01
加载中...