/ tabris /

记录详情

Accepted


  
# 状态 耗时 内存占用
#1 Accepted 2ms 2.57 MiB
#2 Accepted 2ms 2.574 MiB
#3 Accepted 39ms 2.566 MiB
#4 Accepted 1ms 2.57 MiB
#5 Accepted 8ms 2.562 MiB
#6 Accepted 184ms 2.566 MiB

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
inline int read(){
	int x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
int fa[200005],num[200005],r[200005],n,m,t;
char a[10];
inline int find(int x){
	if(fa[x]==x) return x;
	else return fa[x]=find(fa[x]);
}
inline void init(){
	memset(fa,0,sizeof(fa));
	memset(r,0,sizeof(r));
	memset(num,0,sizeof(num));
}
int main(){
	t=read();
	for(int i=1;i<=t;i++){
		printf("Case #%d:\n",i);
		init();
		n=read(); m=read();
		int ans=n,cnt=n;
		for(int j=1;j<=n;++j) r[j]=j,fa[j]=j,num[j]=1;
		while(m--){
			scanf("%s",a);
			if(a[0]=='q'){
				printf("%d\n",ans);
			}
			if(a[0]=='f'){
				int x=read(),y=read();
				int fx=find(r[x]),fy=find(r[y]);
				if(fx==fy) continue;
				else if(num[fx]>num[fy]){
					printf("%d is winner!\n",x);
					fa[fy]=fx;
					num[fx]+=num[fy];
					ans--;
				}
				else if(num[fy]>num[fx]){
					printf("%d is winner!\n",y);
					fa[fx]=fy;
					num[fy]+=num[fx];
					ans--;
				}
				else if(num[fx]==num[fy]) printf("Either is winner!\n");
			}
			if(a[0]=='t'){
				int x=read(),y=read();
				int fx=find(r[x]),fy=find(r[y]);
				if(fx!=fy){
					r[y]=++cnt;
					fa[r[y]]=fx;
					if(num[fy]==1) ans--;
					num[fy]--;
					num[fx]++;
				}
			}
			if(a[0]=='r'){
				int x=read();
				int fx=find(r[x]);
				if(num[fx]==1) continue;
				num[fx]--;
				r[x]=++cnt;
				fa[r[x]]=r[x];
				num[r[x]]=1;
				ans++;
			}
		}
	}
}

信息

递交者
类型
递交
题目
幼儿园战争
语言
C++
递交时间
2019-10-02 19:43:19
评测时间
2019-10-02 19:43:19
评测机
分数
192
总耗时
239ms
峰值内存
2.574 MiB