有了map,就很容易解决了。
题目
求两个人的关系
分析
并查集
代码
#include <iostream>
#include <map>
using namespace std;
map<string,string>uk; string s;
string getf(string u){return (uk[u]==u)?u:uk[u]=getf(uk[u]);}
int main(){
ios::sync_with_stdio(0);
char x; string g;
while (cin>>x,x!='$'){
cin>>s;
if (x=='#'){
g=s; if (uk[s]=="") uk[s]=s;
}
else if (x=='+') uk[s]=g;
else cout<<s<<" "<<getf(s)<<endl;
}
return 0;
}