全0求助QWQ
  • 板块P2814 家谱
  • 楼主DaShabby
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/11/27 13:38
  • 上次更新2023/10/27 01:14:00
查看原帖
全0求助QWQ
672837
DaShabby楼主2022/11/27 13:38
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
const int maxn=5e3+45,inf=0x3f3f3f3f;
//int fa[maxn],tot;
map<string,string>fa;
map<string,int>vis;
string find(string a){
	if(fa[a]!=a)return fa[a]=find(fa[a]);
	return a;
}
void unionn(string a,string b){
	a=find(a);b=find(b);
	fa[a]=b;
}
void work(){
	char ch=getchar();
	string fath;
	while(ch!='$'){
		string s;
		char st=getchar();
		while(st!='\n'){
			s+=st;st=getchar();
		}
//		cout<<s<<endl;
		vis[s]++;
		if(vis[s]==1)fa[s]=s;
		if(ch=='#')fath=s;
		else if(ch=='+')fa[s]=fath;
		else cout<<s<<' '<<find(s)<<endl;
		ch=getchar();
	}
}
int main()
{
	work();
	return 0;;
}
2022/11/27 13:38
加载中...