Test instructions: To a tree of names, the output by the layer, the same layer by the name of the dictionary sequence output.
Idea: First of all to index each person's name, determine which layer, according to the layer loaded into a set, and then the output of the layer automatically ordered.
1#include <bits/stdc++.h>2 using namespacestd;3vector<Set<string> > Ord;//one set per layer, automatically sorted by dictionary order4map<string,int> mapp;//used for hashing5 6 intMain ()7 {8 //freopen ("Input.txt", "R", stdin);9 intN;Ten stringSon,far; One Set<string>tmp; A while(cin>>N) - { - ord.clear (); the for(intI=0; i<=n; i++) Ord.push_back (TMP);//Initialize - mapp.clear (); -mapp["Alice"]=0; - intk=0; + for(intI=0; i<n; i++) - { +Cin>>son>>Far ; Ak=Mapp[far]; atMapp[son]=++k;//son's layer is the number of layers in the far node +1 . - Ord[k].insert (son); - } - - for(intI=1; i<=n; i++) - { in if(Ord[i].empty () = =true) Break; - Else to { + for(Set<string>::iterator It=ord[i].begin (); It!=ord[i].end (); it++) -cout<<*it<<" "<<i<<Endl; the } * } $ }Panax Notoginseng - return 0; the}
AC Code
Nbut 1114 Alice ' s puppets (sort statistics, water)