Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <memory.h>
- #include <iterator>
- #include <cassert>
- #include <fstream>
- #include <iomanip>
- #include <cstdlib>
- #include <bitset>
- #include <vector>
- #include <cstdio>
- #include <string>
- #include <queue>
- #include <deque>
- #include <cmath>
- #include <ctime>
- #include <stack>
- #include <set>
- #include <map>
- using namespace std;
- const int N = (int)2e5 + 10;
- int n;
- map <string , vector<string> > g;
- vector < pair < string , int > > ans;
- void dfs(string name, int dep) {
- ans.push_back(make_pair(name, dep));
- for(int i = 0; i < g[name].size(); ++i) {
- string to = g[name][i];
- dfs(to, dep + 1);
- }
- }
- bool comp(pair<string, int> a, pair<string, int> b) {
- if(a.second != b.second) return a.second > b.second;
- return a.first < b.first;
- }
- int main ()
- {
- //freopen("input.txt" , "r" , stdin);
- //freopen("output.txt" , "w" , stdout);
- ios_base::sync_with_stdio(false);
- cin >> n;
- for(int i = 0; i < n; ++i) {
- string a, b;
- cin >> a >> b;
- g[b].push_back(a);
- }
- dfs("X", 0);
- sort(ans.begin(), ans.end(), comp);
- for(auto &it : ans) {
- cout << it.first << ' ' << it.second << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement