1 / 6
Feb 2022


Here is my code can anybody detect any error in my code …please tell me…it tells no suspicious bug found in every time

#include <bits/stdc++.h>
using namespace std;
vector ar[1000001];
int vis[1000001];
int col[1000001];
bool dfs(int pos, int c)
{
vis[pos] = 1;
col[pos] = c;
for (int child : ar[pos])
{
if (vis[child] == 0) {
bool xy = dfs(child, c ^ 1);
if (xy == false)
return false;
}
else if (col[child] == col[pos]) {
return false;
}
}

}
signed main()
{
ios::sync_with_stdio(0);
cout.tie(0);
int t, n, e, a, b, dd = 1;
cin >> t;
while (t–) {
cin >> n >> e;
int xx = e;
for (int i = 1; i <= n; i++)
ar[i].clear(), vis[i] = 0, col[i] = 0;
while (xx–)
{
cin >> a >> b;
cin.tie(0);
ar[a].push_back(b);
ar[b].push_back(a);
} bool flag = true;
for (int i = 1; i <= n; i++)
{
if ( vis[i] = 0)
{
bool ff = dfs(i, 0);
if (ff == false)
flag = false;
}
}
cout << “Scenario #” << dd << “:” << “\n”; dd++;
//cout << e << n;
if (flag == true )
cout << “No suspicious bugs found!” << “\n”;
else
cout << “Suspicious bugs found!” << “\n”;
}
}

  • created

    Feb '22
  • last reply

    Mar '22
  • 5

    replies

  • 679

    views

  • 2

    users

  • 1

    link

When posting code, please put three back ticks ``` on a line by themselves both before and after the code. This stops the smart editor from interpreting it, as well as preserving the indentation.

Take another look at this line

BUGLIFE3

‘’’#include <bits/stdc++.h>
using namespace std;
vector ar[10000];
int vis[10000];
int col[10000];
bool dfs(int pos, int c)
{
vis[pos] = 1;
col[pos] = c;
for (int child : ar[pos])
{
if (vis[child] == 0) {
bool xy = dfs(child, c ^ 1);
if (xy == true)
return true;
}
else if (col[child] == col[pos]) {
return true;
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cout.tie(0);
int t, n, e, a, b, dd = 1;
cin >> t;
while (t–) {
cin >> n >> e;
int xx = e;
for (int i = 1; i <= n; i++)
ar[i].clear(), vis[i] = 0, col[i] = 0;
while (xx–)
{
cin >> a >> b;
cin.tie(0);
ar[a].push_back(b);
ar[b].push_back(a);
} bool flag = false;
for (int i = 1; i <= n; i++)
{
if ( vis[i] == 0)
{
bool ff = dfs(i, 0);
if (ff == true)
flag = true;
}
}
cout << “Scenario #” << dd << “:” << “\n”; dd++;
//cout << e << n;
if (flag == false )
cout << “No suspicious bugs found!” << “\n”;
else
cout << “Suspicious bugs found!” << “\n”;
}
}’’'
now it gives runtime error sigsegv(Segmentation fault)

…please put three back ticks ``` on a line by themselves both before and after the code…

Thanks very much. I forgot to return false in dfs function at last. Now it runs successfully.