Advertisement
SorahISA

[HLHSOJ c005] topo sort 2

May 14th, 2020
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. // #pragma GCC target("avx2")
  2. #pragma GCC optimize("O3", "unroll-loops")
  3.  
  4. // #include <bits/extc++.h>
  5. // using namespace __gnu_pbds;
  6.  
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9.  
  10. #define int long long
  11. #define double long double
  12. // template <typename T>
  13. // using pbds_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  14. using pii = pair<int, int>;
  15. template<typename T>
  16. using prior = priority_queue<T, vector<T>, greater<T>>;
  17. template<typename T>
  18. using Prior = priority_queue<T>;
  19.  
  20. #define X first
  21. #define Y second
  22. #define ALL(x) (x).begin(), (x).end()
  23. #define eb emplace_back
  24. #define pb push_back
  25.  
  26. #define fastIO() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
  27. #define RANDOM() random_device __rd; \
  28.                  mt19937 __gen = mt19937(__rd()); \
  29.                  uniform_int_distribution<int> __dis(0, 1); \
  30.                  auto rnd = bind(__dis, __gen);
  31.  
  32. const int INF = 1E18;
  33. const int mod = 1E9 + 7;
  34.  
  35. int fl_multi = 0, fl_noans = 0;
  36. int ans_getans = 0, ans_noans = 0;
  37. vector<int> adj[30], InD(30, 0), vis(30, 0);
  38. string seq;
  39.  
  40. void dfs(int now) {
  41.     queue<int> que;
  42.     int cnt = 0;
  43.     for (auto x : adj[now]) {
  44.         if (++vis[x] == InD[x]) ++cnt, que.push(x);
  45.     }
  46.     if (cnt >= 2) fl_multi = 1;
  47.    
  48.     while (!que.empty()) {
  49.         int nxt = que.front(); que.pop();
  50.         seq.pb('A' + nxt), dfs(nxt);
  51.     }
  52. }
  53.  
  54. int32_t main() {
  55.     fastIO();
  56.    
  57.     int n, m;
  58.     cin >> n >> m;
  59.    
  60.     vector<pii> edge(m+1);
  61.     for (int tok = 1; tok <= m; ++tok) {
  62.         char a, b;
  63.         cin >> a >> b;
  64.         edge[tok].X = a - 'A';
  65.         edge[tok].Y = b - 'A';
  66.         ++InD[edge[tok].Y];
  67.         adj[edge[tok].X].eb(edge[tok].Y);
  68.     }
  69.    
  70.     /// find if there's an answer ///
  71.    
  72.     int cnt = 0;
  73.     for (int i = 0; i < n; ++i) {
  74.         if (InD[i] == 0) ++cnt, dfs(i);
  75.     }
  76.     if (cnt >= 2) fl_multi = 1;
  77.    
  78.     for (int i = 0; i < n; ++i) {
  79.         if (vis[i] != InD[i]) fl_noans = 1;
  80.     }
  81.    
  82.     if (fl_multi and !fl_noans) {cout << "No answer\n"; return 0;}
  83.    
  84.     /// init ///
  85.    
  86.     vis.assign(30, 0), InD.assign(30, 0);
  87.     for (int i = 0; i < 30; ++i) adj[i].clear();
  88.    
  89.     /// find where the answer occurs ///
  90.    
  91.     for (int tok = 1; tok <= m; ++tok) {
  92.         vis.assign(30, 0);
  93.         ++InD[edge[tok].Y];
  94.         adj[edge[tok].X].eb(edge[tok].Y);
  95.         cnt = fl_multi = fl_noans = 0;
  96.         seq.clear();
  97.        
  98.         for (int i = 0; i < n; ++i) {
  99.             if (InD[i] == 0) ++cnt, seq.pb('A' + i), dfs(i);
  100.         }
  101.         if (cnt >= 2) fl_multi = 1;
  102.        
  103.         if (!ans_getans and !fl_multi) ans_getans = tok;
  104.        
  105.         for (int i = 0; i < n; ++i) {
  106.             if (vis[i] != InD[i]) fl_noans = 1;
  107.         }
  108.        
  109.         if (!ans_noans and fl_noans) ans_noans = tok;
  110.     }
  111.    
  112.     if (fl_noans) cout << "Order conflict after getting pair " << ans_noans << "\n";
  113.     else cout << "Determine the testing sequence after getting pair " << ans_getans << " : " << seq << "\n";
  114.    
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement