fork(3) download
  1. #include <stdio.h>
  2. #include <stack>
  3. #include <map>
  4. #include <string.h>
  5. #include <string>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <iomanip>
  9. #include <math.h>
  10. #include <vector>
  11. #include <set>
  12. #include <queue>
  13. #include <functional>
  14. using namespace std;
  15. #define ll long long
  16. #define mp make_pair
  17. #define pb push_back
  18. //#define ld long double
  19. const double sn = 1e-6;
  20. int n, m;
  21. int perm[200005];
  22.  
  23. int main() {
  24. int t;
  25. scanf("%d", &t);
  26. for (int ii = 0; ii < t; ii++) {
  27. vector<set<int>> adj = {};
  28. adj.push_back({});
  29. scanf("%d%d", &n, &m);
  30. for (int i = 1; i <= n; i++) {
  31. perm[i] = i;
  32. adj.push_back({});
  33. }
  34. for (int i = 0; i < m; i++) {
  35. int a, b;
  36. scanf("%d%d", &a, &b);
  37. adj[b].insert(a);
  38. }
  39. for (int i = 2; i <= n; i++) {
  40. int j = i;
  41. while (j > 1 && adj[i].find(perm[j-1])!=adj[i].end()) {
  42. int temp = perm[j - 1];
  43. perm[j - 1] = i;
  44. perm[j] = temp;
  45. j--;
  46. }
  47. }
  48. for (int i = 1; i <= n; i++) {
  49. printf("%d", perm[i]);
  50. if (i != n)
  51. printf(" ");
  52. }
  53. printf("\n");
  54. }
  55. return 0;
  56. }
Success #stdin #stdout 0s 16848KB
stdin
Standard input is empty
stdout
Standard output is empty