1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #include<bits/stdc++.h> // 万能头文件
using namespace std;
int f[50100]; map <string, int> Num; string Names[50100]; map <string, bool> Used;
int Find(int x){ if(f[x] == x) return x; int next = Find(f[x]); f[x] = next; return next; }
void Link(int Fa, int Son){ f[Son] = f[Find(Fa)]; }
int main(){ string Name; string Fa; char Type = 'A'; int Cur = 0; int Fanum; while(Type != '$'){ Type = getchar(); switch(Type){ case '#':{ cin >> Fa; if(!Used[Fa]){ Used[Fa] = true; Num[Fa] = ++Cur; Names[Cur] = Fa; } if(f[Num[Fa]] == 0) f[Cur] = Cur; Fanum = f[Num[Fa]]; break; } case '+':{ cin >> Name; if(!Used[Name]){ Used[Name] = true; Num[Name] = ++Cur; Names[Cur] = Name; } if(f[Num[Name]] == 0) f[Num[Name]] = Cur; Link(Fanum, Num[Name]); break; } case '?':{ cin >> Name; cout << Name << " " << Names[Find(Num[Name])]<< endl; break; } default:break; } } return 0; }
|