1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include<bits/stdc++.h> using namespace std; typedef long long ll;
class Solution { unordered_map<int, int> fa_map; public: int findFa(int x, vector<int>& fa) { return fa[x] < 0 ? x : fa[x] = findFa(fa[x], fa); }
void unit(int x, int y, vector<int>& fa) { x = findFa(x, fa); y = findFa(y, fa); if (x == y) { return ; } if (fa[x] < fa[y]) { swap(x, y); } fa[x] += fa[y]; fa[y] = x; }
bool isconnect(int x, int y, vector<int>& fa) { x = findFa(x, fa); y = findFa(y, fa); return x == y; }
bool possibleBipartition(int n, vector<vector<int>>& dislikes) { vector<int> fa(n + 1, -1); vector<vector<int>> g(n + 1); for (auto& p : dislikes) { g[p[0]].push_back(p[1]); g[p[1]].push_back(p[0]); } for (int i = 1; i <= n; ++i) { for (int j = 0; j < g[i].size(); ++j) { unit(g[i][0], g[i][j], fa); if (isconnect(i, g[i][j], fa)) { return false; } } } return true; } };
int main(){ return 0; }
|