【问题标题】:BFS algorithm for checking bipartite graph用于检查二分图的 BFS 算法
【发布时间】:2021-12-04 17:50:13
【问题描述】:

我很困惑为什么我的代码会产生错误的输出。如果每个节点还没有被访问过,我会尝试为每个节点执行 bfs,并尝试访问它的 adj 节点。谁能帮我找到问题? 这是我的代码:-

#include <bits/stdc++.h>
using namespace std;
int p = 1;
int ans[200005] = {-1};
bool bfs(vector<int> graph[], int x, int ans[])
{
    queue<int> q;
    q.push(x);
    ans[x] = 1;
    while (!q.empty())
    {
        int f = q.front();
        q.pop();
        for (auto it : graph[f])
        {
            if (ans[it] == -1)
                ans[it] = 1 - ans[f], q.push(it);
            else if (ans[it] == ans[f])
                return true;
        }
    }
    return false;
}
signed main()
{
    int n, m;
    cin >> n >> m;
    vector<int> graph[n + 1];
    for (int i = 1, a, b; i <= m; i++)
    {
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    for (int i = 1; i <= n; i++)
    {
        if (ans[i] == -1)
        {
            if (bfs(graph, i, ans))
                p = 0;
        }
    }
    if (!p)
        cout << "IMPOSSIBLE";
    else
    {
        for (int i = 1; i <= n; i++)
        {
            cout << ans[i] + 1 << ' ';
        }
    }
}

输入:-

5 3
1 2
1 3
4 5

预期输出:-

1 2 2 1 2

收到的输出:-

1 1 1 1 1

【问题讨论】:

  • vector&lt;int&gt; graph[n + 1]; 在符合标准的 C++ 中无效。为什么不vector&lt;vector&lt;int&gt;&gt; graph(n + 1)
  • 你为什么使用bits/stdc++.h?那只会鼓励你写草率的代码。
  • 不要使用神秘和简短的变量名。您的程序代码应该是自记录的。此外,您的代码混合了惯用的 C 和 C++ 风格,并且您正在改变全局状态。这里有很多改进...
  • 请提供示例输入、该输入的预期输出以及您得到的结果。
  • 另外,“我很困惑为什么我的代码产生错误的输出。” 到底是什么意思?你的代码产生了什么输出,你期望它产生什么?

标签: c++ algorithm data-structures


【解决方案1】:

你的算法很好

C 数组初始化不会从只包含 1 个元素的花括号列表初始化程序初始化整个数组,除了它是 0。

int arr1[5] = {-1}; // -1, 0, 0, 0, 0
int arr1[5] = {0}; // 0, 0, 0, 0, 0

https://stackoverflow.com/a/1065800/10911830

这里是固定代码,为了不浪费屏幕空间,压缩了主函数:

#include <vector> // for input
#include <iostream> // for debugging
#include <queue> // for bfs
#include <algorithm> // for std::fill_n
using namespace std;

bool bfs(vector<int> graph[], int x, int ans[])
{
    queue<int> q;
    q.push(x);
    ans[x] = 1;
    while (!q.empty())
    {
        int f = q.front();
        q.pop();
        for (auto it : graph[f])
        {
            if (ans[it] == -1){
                ans[it] = 1 - ans[f];
                q.push(it);
            }
            else if (ans[it] == ans[f])
                return true;
        }
    }
    return false;
}
int main()
{   
    int p = 1;
    const int ANS_MAX = 200005;
    int ans[ANS_MAX];
    std::fill_n(ans, ANS_MAX , -1); // 1. array initialization
    int n, m;
    cin >> n >> m;
    vector<int> graph[n + 1];
    for (int i = 1, a, b; i <= m; i++)
    {
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    for (int i = 1; i <= n; i++) 
        if (ans[i] == -1 && bfs(graph, i, ans)) p = 0;

    if (!p) cout << "IMPOSSIBLE";
    else for (int i = 1; i <= n; i++) cout << ans[i] + 1 << ' ';
    
   }

我没有使用任何其他输入对其进行测试,因此可能存在一些错误,但至少现在主要问题已修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    相关资源
    最近更新 更多