Norvager

http://lerna.pro/contests/84/11

Feb 10th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <cmath>
  5. #include <vector>
  6. #include <queue>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. vector<vector<int>> lin;
  12. int cur_max_number;
  13. vector<int> ans, use1, use2;
  14.  
  15. bool dfs(int c)
  16. {
  17.     if (use1[c] && ans[c] == 0)
  18.         return false;
  19.     use1[c] = true;
  20.     bool sortable = true;
  21.     for (int i = 1; i < lin[c].size(); i++)
  22.     {
  23.  
  24.         if (!use1[lin[c][i]])
  25.             sortable = sortable && dfs(lin[c][i]);
  26.         else if (!use2[lin[c][i]])
  27.         {
  28.             return false;
  29.         }
  30.     }
  31.     use2[c] = true;
  32.     ans[cur_max_number] = c;
  33.     cur_max_number -= 1;
  34.     return sortable;
  35. }
  36.  
  37. int main()
  38. {
  39.     int n, m, a, b, d;
  40.     scanf("%d %d", &n, &m);
  41.     cur_max_number = n;
  42.     lin.resize(n + 1, vector<int>(1));
  43.     for (int i = 0; i < m; i++)
  44.     {
  45.         scanf("%d %d", &a, &b);
  46.         lin[a].push_back(b);
  47.     }
  48.     ans.resize(n + 1, 0);
  49.     use1.resize(n + 1, false);
  50.     use2.resize(n + 1, false);
  51.     for (int i = 1; i <= n; i++)
  52.         if (!use1[i])
  53.             if (!dfs(i))
  54.             {
  55.                 printf("No");
  56.                 return 0;
  57.             }
  58.  
  59.     printf("Yes\n");
  60.     for (int i = 1; i <= n; i++)
  61.         printf("%d ", ans[i]);
  62.  
  63.     return 0;
  64. }
Add Comment
Please, Sign In to add comment