Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <map>
- #include <vector>
- #include <algorithm>
- using namespace std;
- map <string, int> used;
- map < string , map <string, int> > nodes;
- vector <string> groups;
- void findgroups(string node,int level) {
- if (!used[node]) {
- if (level == 0 ) groups.push_back("");
- used[node] = 1;
- groups.back() += node;
- map <string, int>::iterator jt;
- for (jt = nodes[node].begin(); jt != nodes[node].end() ; jt++) {
- findgroups(jt->first,level+1);
- }
- }
- }
- int main() {
- int n;
- cin >> n;
- string src, ref;
- for (int i = 0 ; i < n ; i++) {
- cin >> src >> ref;
- used[src] = used[ref] = 0;
- nodes[src][ref] = nodes[ref][src] = 1;
- }
- map < string , int >::iterator nk;
- for (nk = used.begin() ; nk != used.end(); nk++) {
- findgroups(nk->first,0);
- }
- vector <string>::iterator git;
- sort(groups.begin(),groups.end());
- for (git = groups.begin(); git != groups.end(); git++) {
- sort(git->begin(),git->end());
- cout << *git << endl;
- }
- // system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement