超时咋办
  • 板块P2814 家谱
  • 楼主chenkuo
  • 当前回复4
  • 已保存回复4
  • 发布时间2022/7/8 17:38
  • 上次更新2023/10/27 21:28:06
查看原帖
超时咋办
443023
chenkuo楼主2022/7/8 17:38

最后两个点超时,大佬帮忙优化一下,谢谢

#include<bits/stdc++.h>
using namespace std;
char a;
string s,ss[100000];
int t,g,w,i,f[100000];
int zhao(string s){
	int i;
	for(i=1;i<=w;i++) if(ss[i]==s) return i;
	ss[++w]=s;
	return w;
}
int find(int x){
	if(f[x]!=x) f[x]=find(f[x]);
	return f[x];
}
int main(){
	for(i=1;i<=50000;i++) f[i]=i;
	while(1){
		cin>>a;
		if(a=='$') break;
		cin>>s;
		if(a=='#') g=zhao(s);
		if(a=='+'){
			t=zhao(s);
			f[find(t)]=f[find(g)];
		}
		if(a=='?') cout<<s<<' '<<ss[find(zhao(s))]<<endl;  
	}
}

2022/7/8 17:38
加载中...