#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
string name;
int father;
};
node a[50009];
int findx(int i)
{
if(a[i].father!=i)a[i].father=findx(a[i].father);
return a[i].father;
}
void unionn(int r,int t)
{
int q=findx(t);
if(r!=q)a[q].father=r;
}
int main()
{
char c;
int tot=0;
int father;
string s;
do
{
cin>>c;
if(c=='$')break;
cin>>s;
int r=-1;
for(int i=1;i<=tot;i++)
if(a[i].name==s)
{
r=i;
break;\\找到就可以跳出;
}
if(r==-1)
{
r=++tot;
a[r].father=r;
a[r].name=s;
}
if(c=='#')
{
father=a[r].father;
continue;
}
if(c=='+')
{
unionn(father,r);
continue;
}
if(c=='?')
cout<<s<<" "<<a[findx(r)].name<<endl;
}while(1);
return 0;
}
1388:家谱(gen)
最新推荐文章于 2023-03-24 17:09:14 发布