Advertisement
SorahISA

[HLHSOJ c005] topo sort 1

May 14th, 2020
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 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, seq_rev;
  39.  
  40. void dfs(int now) {
  41.     for (auto x : adj[now]) {
  42.         if (++vis[x] == InD[x]) seq.pb('A' + x), dfs(x);
  43.     }
  44. }
  45.  
  46. void dfs_rev(int now) {
  47.     for (auto x : adj[now]) {
  48.         if (++vis[x] == InD[x]) seq_rev.pb('A' + x), dfs_rev(x);
  49.     }
  50. }
  51.  
  52. int32_t main() {
  53.     fastIO();
  54.    
  55.     int n, m;
  56.     cin >> n >> m;
  57.    
  58.     vector<pii> edge(m+1);
  59.     for (int tok = 1; tok <= m; ++tok) {
  60.         char a, b;
  61.         cin >> a >> b;
  62.         edge[tok].X = a - 'A';
  63.         edge[tok].Y = b - 'A';
  64.         ++InD[edge[tok].Y];
  65.         adj[edge[tok].X].eb(edge[tok].Y);
  66.     }
  67.    
  68.     /// find if there's multiple answer ///
  69.    
  70.     for (int i = 0; i < n; ++i) {
  71.         if (InD[i] == 0) seq.pb('A' + i), dfs(i);
  72.     }
  73.    
  74.     vis.assign(30, 0);
  75.     for (int i = 0; i < n; ++i) reverse(ALL(adj[i]));
  76.    
  77.     for (int i = n-1; i >= 0; --i) {
  78.         if (InD[i] == 0) seq_rev.pb('A' + i), dfs_rev(i);
  79.     }
  80.    
  81.     fl_multi = seq != seq_rev;
  82.    
  83.     /// find if there's no answer ///
  84.    
  85.     for (int i = 0; i < n; ++i) {
  86.         if (vis[i] != InD[i]) fl_noans = 1;
  87.     }
  88.    
  89.     if (fl_multi and !fl_noans) {cout << "No answer\n"; return 0;}
  90.    
  91.     /// init ///
  92.    
  93.     vis.assign(30, 0), InD.assign(30, 0);
  94.     for (int i = 0; i < 30; ++i) adj[i].clear();
  95.    
  96.     /// find where the answer occurs ///
  97.    
  98.     for (int tok = 1; tok <= m; ++tok) {
  99.        
  100.         /// init ///
  101.        
  102.         vis.assign(30, 0);
  103.         for (int i = 0; i < n; ++i) reverse(ALL(adj[i]));
  104.         ++InD[edge[tok].Y];
  105.         adj[edge[tok].X].eb(edge[tok].Y);
  106.         fl_multi = fl_noans = 0;
  107.         seq = seq_rev = "";
  108.        
  109.         /// do it once, reverse it, do it again ///
  110.        
  111.         for (int i = 0; i < n; ++i) {
  112.             if (InD[i] == 0) seq.pb('A' + i), dfs(i);
  113.         }
  114.        
  115.         vis.assign(30, 0);
  116.         for (int i = 0; i < n; ++i) reverse(ALL(adj[i]));
  117.        
  118.         for (int i = n-1; i >= 0; --i) {
  119.             if (InD[i] == 0) seq_rev.pb('A' + i), dfs_rev(i);
  120.         }
  121.        
  122.         /// determine if there exist an answer ///
  123.        
  124.         fl_multi = seq != seq_rev;
  125.         if (!ans_getans and !fl_multi) ans_getans = tok;
  126.        
  127.         for (int i = 0; i < n; ++i) {
  128.             if (vis[i] != InD[i]) fl_noans = 1;
  129.         }
  130.        
  131.         if (!ans_noans and fl_noans) ans_noans = tok;
  132.     }
  133.    
  134.     if (fl_noans) cout << "Order conflict after getting pair " << ans_noans << "\n";
  135.     else cout << "Determine the testing sequence after getting pair " << ans_getans << " : " << seq << "\n";
  136.    
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement