fork download
  1. #include <cstdio>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int cnt;
  7. int post[100010];
  8. int visit[100010];
  9.  
  10. vector<int> e[100010];
  11.  
  12. void dfs(int u) {
  13. if(visit[u]) return;
  14. visit[u] = 1;
  15. for(auto &v : e[u]) dfs(v);
  16. post[u] = ++cnt;
  17. }
  18.  
  19. int main() {
  20. int n, m1, m2;
  21. scanf("%d %d %d", &n, &m1, &m2);
  22. for(int i = 0; i < m1; i++) {
  23. int x, y;
  24. scanf("%d %d", &x, &y);
  25. e[x].push_back(y);
  26. }
  27. for(int i = 1; i <= n; i++) dfs(i);
  28. for(int i = 0; i < m2; i++) {
  29. int x, y;
  30. scanf("%d %d", &x, &y);
  31. if(post[x] < post[y]) printf("%d %d\n", y, x);
  32. else printf("%d %d\n", x, y);
  33. }
  34. return 0;
  35. }
Success #stdin #stdout 0s 5496KB
stdin
4 2 3
1 2
4 3
1 3
4 2
3 2
stdout
3 1
4 2
3 2