soj4522 完全二分图的判断

题意
有三个字母 a ,b, c ,只有相邻字母或者相同字母才连边。现在给你一个无向图,问你是否是这个规则建立出来的。
分析
- wa了很多次,还是思路没有分析好
- 观察发现只有a,c这对之间不能有边相连接,图2着色问题,就是完全二分图。
- 注意除了孤立点,整个图是个完全二分图。

#include <cstdio>
#include <iostream>
#include <vector>
#include <stack>
#define pr(x) std::cout << #x << ": " << x << " "
#define pl(x) std::cout << #x << ": " << x << std::endl

class Solution 
{
public:
    void input() {
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) for (int j = 1; j < i; j++) Graph_matrix[i][j] = false;
        for (int i = 0; i < m; i++) {
            int x, y;
            scanf("%d%d", &x, &y);
            if (x < y) std::swap(x, y);
            Graph_matrix[x][y] = true;
        }
    }

    void bulid_graph() {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j < i; j++) {
                if (Graph_matrix[i][j] == false) {
                    Graph[i].push_back(j);
                    Graph[j].push_back(i);
                }
            }
        }
    }

    bool dfs(int cur, int c) {
        color[cur] = c; cnt++;
        for (int i = 0; i < (int)Graph[cur].size(); i++) {
            int nxt = Graph[cur][i];
            if (color[nxt] == c) return false;
            if (color[nxt] == 0 && !dfs(nxt, -c)) return false;
        }
        return true;
    }

    void getit(int cur) {
        st.push(cur);
        if (color[cur] == 1) one++;
        else two++;
        vis[cur] = true;
        for (int i = 0; i < (int)Graph[cur].size(); i++) {
            int nxt = Graph[cur][i];
            if (!vis[nxt]) getit(nxt);
        }
    }
    void detect_BipartiteGraph() {
        ans = true;
        for (int i = 1; i <= n; i++) color[i] = 0, vis[i] = false;
        int key = 0;
        for (int i = 1; i <= n; i++) {
            if (!color[i]) {
                cnt = 0;
                ans = dfs(i, 1);
                if (cnt > 1) key++;
            }
            one = 0, two = 0;
            if (!vis[i]) getit(i);
            while (!st.empty()) {
                int cur = st.top();
                if (color[cur] == -1) {
                    if ((int)Graph[cur].size() != one) ans = false;
                } else {
                    if ((int)Graph[cur].size() != two) ans = false;
                }
                st.pop();
            }
            if (!ans) break;
        }
        puts(ans && key <= 1? "Yes" : "No");
    }
private:
    static const int maxn = 503;
    int n, m, one, two, cnt;
    bool Graph_matrix[maxn][maxn], ans, vis[maxn];
    std::vector<int> Graph[maxn];
    std::stack<int> st;
    int color[maxn];
};

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    while (T--) {
        Solution work;
        work.input();
        work.bulid_graph();
        work.detect_BipartiteGraph();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值