1 / 2
May 2020

I am getting a WA on buglife and can’t seem to find the error. Can anyone please help?

#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;
typedef int ll;


struct Node {
    vector<ll> nei;
    void addEdge(ll x) {
        this->nei.push_back(x);
    }
    Node() = default;
};

queue<ll> s;

void bfs(vector<Node> &graph, vector<ll> &label, bool &f) {
    while(!s.empty()) {
        ll n = s.front();
        s.pop();
        for (ll x: graph[n].nei) {
            if (label[x] == -1) {
                label[x] = label[n] + 1;
                s.push(x);
            } else if (label[x] == label[n]) {
                f = true;
                return;
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int t, t2;
    cin>>t;
    t2 = t;
    while (t--) {
        ll n, m;
        cin>>n>>m;
        vector<Node> graph(n);
        ll a, b;
        bool ans = false;
        for(int i=0;i<m;i++) {
            cin>>a>>b;
            a--, b--;
            graph[a].addEdge(b);
            graph[b].addEdge(a);
        }
        vector<ll> label(n, -1);
        for(int i = 0;i<label.size();i++) {
            if(label[i] == -1) {
                label[i] = 0;
                s.push(i);
                bfs(graph, label, ans);
            }
        }
        while(!s.empty()) s.pop();
        printf("Scenario #%d:\n", t2-t);
        if(!ans) {
           cout<<"No suspicious bugs found!\n";
        } else {
            cout<<"Suspicious bugs found!\n";
        }

    }
    return 0;
}
  • created

    May '20
  • last reply

    May '20
  • 1

    reply

  • 524

    views

  • 2

    users

You should use 3 different labels: 0: unmarked, 1: group 1, -1: group 2