Advertisement
Ne-Biolog

Untitled

May 17th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <memory.h>
  4. #include <iterator>
  5. #include <cassert>
  6. #include <fstream>
  7. #include <iomanip>
  8. #include <cstdlib>
  9. #include <bitset>
  10. #include <vector>
  11. #include <cstdio>
  12. #include <string>
  13. #include <queue>
  14. #include <deque>
  15. #include <cmath>
  16. #include <ctime>
  17. #include <stack>
  18. #include <set>
  19. #include <map>
  20.  
  21. using namespace std;
  22.  
  23. const int N = (int)2e5 + 10;
  24.  
  25. int n;
  26. map <string , vector<string> > g;
  27.  
  28. vector < pair < string , int > > ans;
  29.  
  30. void dfs(string name, int dep) {
  31. ans.push_back(make_pair(name, dep));
  32. for(int i = 0; i < g[name].size(); ++i) {
  33. string to = g[name][i];
  34. dfs(to, dep + 1);
  35. }
  36. }
  37.  
  38. bool comp(pair<string, int> a, pair<string, int> b) {
  39. if(a.second != b.second) return a.second > b.second;
  40. return a.first < b.first;
  41. }
  42.  
  43. int main ()
  44. {
  45. //freopen("input.txt" , "r" , stdin);
  46. //freopen("output.txt" , "w" , stdout);
  47. ios_base::sync_with_stdio(false);
  48.  
  49. cin >> n;
  50. for(int i = 0; i < n; ++i) {
  51. string a, b;
  52. cin >> a >> b;
  53. g[b].push_back(a);
  54. }
  55.  
  56. dfs("X", 0);
  57.  
  58. sort(ans.begin(), ans.end(), comp);
  59.  
  60. for(auto &it : ans) {
  61. cout << it.first << ' ' << it.second << endl;
  62. }
  63.  
  64.  
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement