Advertisement
pb_jiang

ABC327D WA

Nov 15th, 2023
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #ifndef __DEBUG__
  5. #define dbg(...) 42
  6. #endif
  7. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  8.  
  9. using ll = long long;
  10. using pii = pair<int, int>;
  11. using pll = pair<ll, ll>;
  12. using vl = vector<ll>;
  13. using vi = vector<int>;
  14.  
  15. int main(int argc, char **argv)
  16. {
  17.     int n, m;
  18.     cin >> n >> m;
  19.     vi a(m), b(m);
  20.     for (auto &x : a)
  21.         cin >> x;
  22.     for (auto &x : b)
  23.         cin >> x;
  24.     vi pa(n + 1, -1), sign(n + 1);
  25.     function<int(int)> ufind = [&](int x) {
  26.         if (pa[x] == -1)
  27.             return x;
  28.         int v = ufind(pa[x]);
  29.         sign[x] = sign[x] ? 1 - sign[v] : sign[v];
  30.         return pa[x] = v;
  31.     };
  32.     auto ujoin = [&](int x, int y) {
  33.         int fx = ufind(x), fy = ufind(y);
  34.         if (fx == fy) {
  35.             return sign[x] != sign[y];
  36.         }
  37.         sign[fy] = sign[x] == sign[y] ? 1 - sign[fy] : sign[fy];
  38.         pa[fy] = fx;
  39.         return true;
  40.     };
  41.     bool ans = true;
  42.     for (int i = 0; i < m && ans; ++i)
  43.         ans = ans && ujoin(a[i], b[i]);
  44.     cout << (ans ? "Yes" : "No") << endl;
  45.     return 0;
  46. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement