Korotkodul

ЗОШ Unique TopSOrt Idee

Jan 10th, 2022 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. using namespace std;
  12. using ll = long long;
  13. using ld = long double;
  14. void cv(vector <int> &v){
  15. for (auto x: v) cout<<x<<' ';
  16. cout<<"\n";
  17. }
  18.  
  19. void cvl(vector <ll> &v){
  20. for (auto x: v) cout<<x<<' ';
  21. cout<<"\n";
  22. }
  23.  
  24.  
  25. void cvv(vector <vector <int> > &v){
  26. for (auto x: v) cv(x);
  27. cout<<"\n";
  28. }
  29. int n,m;
  30. vector <vector<int>>G;
  31. const int wh = 0, gr = 1, bl = 2;
  32. vector <int> clr;
  33. vector <int> tp;
  34.  
  35. //int cnt = 0;//connected
  36. void dfs(int v, int pr){
  37. clr[v] = gr;
  38. for (int u: G[v]){
  39. if (u == pr) continue;
  40. else if (clr[u] == wh){
  41. dfs(u, v);
  42. for (int x: G[v]){
  43. if (clr[x] != bl){
  44. cout<<"NO\n";
  45. exit(0);
  46. }
  47. }
  48. }
  49. else if (clr[u] == gr){
  50. cout<<"NO\n";
  51. exit(0);
  52. }
  53. }
  54. clr[v] = bl;
  55. tp.push_back(v+1);
  56. }
  57.  
  58.  
  59.  
  60.  
  61. int main()
  62. {
  63. ios::sync_with_stdio(0);
  64. cin.tie(0);
  65. cout.tie(0);
  66. cin>>n>>m;
  67. G.resize(n);
  68. clr.assign(n, wh);
  69. vector <int> in(n,0), out(n, 0);
  70. bool can = 1;
  71. for (int i=0;i<m;++i){
  72. int a,b; cin>>a>>b;
  73. a--; b--;
  74. G[a].push_back(b);
  75. in[b]++;
  76. out[a]++;
  77. }
  78. if (count(in.begin(), in.end(), 0) != 1){
  79. can = 0;
  80. }
  81. if (count(out.begin(), out.end(), 0) != 1){
  82. can = 0;
  83. }
  84. if (!can){
  85. cout<<"NO\n";
  86. exit(0);
  87. }
  88. for (int i = 0;i<n;++i){
  89. if (clr[i] == wh){
  90. dfs(i, -1);
  91. }
  92. }
  93. cout<<"YES\n";
  94. reverse(tp.begin(), tp.end());
  95. cv(tp);
  96. }
  97. /*
  98. КОНТРПРИМЕР:
  99. 3 3
  100. 1 2
  101. 2 3
  102. 1 3
  103. YES
  104. 1 2 3
  105. */
Add Comment
Please, Sign In to add comment