这题我用并查集 + map做,全RE。
调试了一下,父子关系似乎没错(?)
报错信息是double free or corruption (out)
bdfs无果,求调
#include <iostream>
#include <map>
#define N 500100
using namespace std;
map <string,string> p;
char op;
string name,dad;
string found(string k){
if(k == p[k]) return k;
else p[k] = found(p[k]);
}
void print(string str){
int len = str.size();
for(int i = 0;i < len;i ++){
cout << str[i];
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
while(cin >> op){
if(op == '$') break;
cin >> name;
if(p[name] == "") p[name] = name;
if(op == '#'){
dad = name;
}
if(op == '+'){
p[name] = dad;
}
if(op == '?'){
print(name);cout << ' ';
print(found(name)); cout << '\n';
}
}
return 0;
}