785. 判断二分图
题目:
链接https://leetcode.cn/problems/is-graph-bipartite/
代码:
class Solution {
public:
int f[205];
set<int> s;
int getf (int x){
if (f[x] == x) return x;
return f[x] = getf(f[x]);
}
bool mer(int x ,int y){
int fx = getf(x),fy = getf(y);
if(fx == fy) return false;
if(fx > fy) swap(fx,fy);
f[fx] = fy;
return true;
}
bool isBipartite(vector<vector<int>>& graph) {
int n = graph.size();
for(int i = 0;i < n;i++){
f[i]=i;
}
for(int i = 0;i < n;i++){
for(int j = 1;j < graph[i].size();j++){
if(getf(i)==getf(graph[i][j]))return 0;
mer(graph[i][0],graph[i][j]);
}
}
return 1;
}
};