并查集,用map映射下,然后记得记录父亲
cpp#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
map<string, string> p;
string find(string x) {
if (x != p[x])
p[x] = find(p[x]);
return p[x];
}
string s, s1;
int main() {
char ch;
cin >> ch;
while (ch != '$') {
cin >> s;
if (ch == '#') {
s1 = s;
if (p[s] == "") p[s] = s;
} else if (ch == '+') {
p[s] = s1;
} else {
cout << s << ' ' << find(s) << endl;
}
cin >> ch;
}
return 0;
}
本文作者:yowayimono
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!