Advertisement
SorahISA

[HDU 3062] Party (2-sat)

Apr 6th, 2020
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. /* http://acm.hdu.edu.cn/showproblem.php?pid=3062 */
  2.  
  3. // #pragma GCC target("avx2")
  4. #pragma GCC optimize("O3", "unroll-loops")
  5.  
  6. // #include <bits/extc++.h>
  7. // using namespace __gnu_pbds;
  8.  
  9. // #include <bits/stdc++.h>
  10. #include <cstdint>
  11. #include <cstdio>
  12. #include <cstring>
  13. #include <queue>
  14. #include <vector>
  15. using namespace std;
  16.  
  17. // #define int long long
  18. #define double long double
  19. // template <typename T>
  20. // using pbds_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  21. // using pii = pair<int, int>;
  22. // template<typename T>
  23. // using prior = priority_queue<T, vector<T>, greater<T>>;
  24. // template<typename T>
  25. // using Prior = priority_queue<T>;
  26.  
  27. #define X first
  28. #define Y second
  29. #define ALL(x) (x).begin(), (x).end()
  30. #define eb emplace_back
  31. #define pb push_back
  32.  
  33. #define fastIO() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
  34. #define RANDOM() random_device __rd; \
  35.                  mt19937 __gen = mt19937(__rd()); \
  36.                  uniform_int_distribution<int> __dis(0, 1); \
  37.                  auto rnd = bind(__dis, __gen);
  38.  
  39. const int INF = 1E18;
  40. const int mod = 1E9 + 7;
  41. const int maxn = 2000 + 5;
  42.  
  43. int scc[maxn], vis[maxn];
  44. vector<int> order;
  45. vector<int> adj[maxn], rev[maxn];
  46.  
  47. void dfs_rev(int now) {
  48.     vis[now] = 1;
  49.     for (auto x : rev[now]) {
  50.         if (!vis[x]) dfs_rev(x);
  51.     }
  52.     order.eb(now);
  53. }
  54.  
  55. void dfs(int now, int scc_cnt) {
  56.     scc[now] = scc_cnt;
  57.     for (auto x : adj[now]) {
  58.         if (scc[x] == -1) dfs(x, scc_cnt);
  59.     }
  60. }
  61.  
  62. void kosaraju(int n) {
  63.     memset(vis, 0x00, sizeof(vis));
  64.     memset(scc, 0xff, sizeof(scc));
  65.     order.clear();
  66.    
  67.     for (int i = 0; i < n; ++i) {
  68.         if (!vis[i]) dfs_rev(i);
  69.     }
  70.    
  71.     int scc_cnt = 1;
  72.     for (int i = n-1; i >= 0; --i) {
  73.         int now = order[i];
  74.         if (scc[now] == -1) dfs(now, scc_cnt++);
  75.     }
  76. }
  77.  
  78. void solve(int n, int m) {
  79.     for (int i = 0; i < 2*n; ++i) adj[i].clear(), rev[i].clear();
  80.    
  81.     for (int i = 0; i < m; ++i) {
  82.         int a1, a2, c1, c2, id1, id2;
  83.         scanf("%d%d%d%d", &a1, &a2, &c1, &c2);
  84.         id1 = 2*a1 + c1, id2 = 2*a2 + c2;
  85.         adj[id1].eb(id2^1), rev[id2^1].eb(id1);
  86.         adj[id2].eb(id1^1), rev[id1^1].eb(id2);
  87.     }
  88.    
  89.     kosaraju(2 * n);
  90.    
  91.     for (int i = 0; i < 2*n; i += 2) {
  92.         // cerr << scc[i] << " " << scc[i^1] << "\n";
  93.         if (scc[i] == scc[i^1]) {puts("NO"); return;}
  94.     }
  95.    
  96.     puts("YES");
  97. }
  98.  
  99. int32_t main() {
  100.     // fastIO();
  101.    
  102.     int n, m;
  103.     while (scanf("%d%d", &n, &m) != EOF) solve(n, m);
  104.    
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement