Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <bits/stdc++.h>
- using namespace std;
- #ifndef __DEBUG__
- #define dbg(...) 42
- #endif
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- using ll = long long;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- using vl = vector<ll>;
- using vi = vector<int>;
- int main(int argc, char **argv)
- {
- int n, m;
- cin >> n >> m;
- vi a(m), b(m);
- for (auto &x : a)
- cin >> x;
- for (auto &x : b)
- cin >> x;
- vi pa(n + 1, -1), sign(n + 1);
- function<int(int)> ufind = [&](int x) {
- if (pa[x] == -1)
- return x;
- int v = ufind(pa[x]);
- sign[x] = sign[x] ? 1 - sign[v] : sign[v];
- return pa[x] = v;
- };
- auto ujoin = [&](int x, int y) {
- int fx = ufind(x), fy = ufind(y);
- if (fx == fy) {
- return sign[x] != sign[y];
- }
- sign[fy] = sign[x] == sign[y] ? 1 - sign[fy] : sign[fy];
- pa[fy] = fx;
- return true;
- };
- bool ans = true;
- for (int i = 0; i < m && ans; ++i)
- ans = ans && ujoin(a[i], b[i]);
- cout << (ans ? "Yes" : "No") << endl;
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement