Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE
Showing posts with label Graph. Show all posts
Showing posts with label Graph. Show all posts

Saturday, October 10, 2015

SHPATH-The Shortest Path

The Shortest Path

Given Below code is for shpath spoj or the shortest path spoj.



/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 08 September 2015 (Tuesday) 20:11
===================================================*/
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define ll long long
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define mod 1000000009
struct cmp
{
    bool operator()(const pair<int , int> &a , const pair<int , int> &b){
        return a.first > b.first;
    }
};
int main(){
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        string s;
        vector<int> graph[n+9];
        vector<vector<int> > cost(n+1 , vector<int>(n+1 , 0));
        map<string, int> map_name;
        for(int i = 1 ; i <= n ; i++){
            cin>>s;
            map_name[s] = i;
            int path , k , c;
            cin>>path;
            for(int j = 0 ; j < path ; j++){
                cin>>k>>c;
                graph[i].pb(k);
                cost[i][k] = c;
            }
        }
        int k;
        cin>>k;
        while(k--){
            char source_temp[12] , dest_temp[12];
            string source , dest;
            source.assign(source_temp);
            dest.assign(dest_temp);
            cin>>source>>dest;
            priority_queue<pii , vector<pii >, cmp > pq;
            ll dist[n+9];
            fill(dist , dist+n , INT_MAX-100000);
            int dist_num = map_name[dest] , source_num = map_name[source];
            dist[source_num] = 0;
            pq.push(mp(0 , source_num));
            while(!pq.empty()){
                int wt = pq.top().first;
                int node = pq.top().second;
                pq.pop();
                if(node == dist_num){
                    cout<<wt<<endl;
                    break;
                }
                for(auto it:graph[node]){
                    if(dist[it] > dist[node] + cost[node][it]){
                        dist[it] = dist[node] + cost[node][it];
                        pq.push(mp(dist[it] , it));
                    }
                }
            }
        }
    }
    
    return 0;
}

Monday, August 17, 2015

COMCB-Complete Chess Boards

Complete Chess Boards

Given below c++11 code is for COMCB spoj or Complete Chess Boards spoj.

Hint :- dfs with backtracking ,
for each node apply DFS and check if traversal of chess board is possible or not . Inside send child node in lexicographical order so that final path will be lexicographically shortest.


/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 17 August 2015 (Monday) 04:58
===================================================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define mod 1000000009
int x[8] = {-1,1,-2,2,-2,2,-1,1};
int y[8] = {-2,-2,-1,-1,1,1,2,2};
int n , m;
bool valid(int row , int col){
    if(row>=0 && row< n && col >=0 && col < m)
        return true;
    return false;
}
bool dfs(vector<string> path ,int row ,int col , int count, int check[][30]){
    string temp = "";
    char c = (char)(col + 65);
    temp.pb(c);
    temp.append(to_string(row+1));
    path.pb(temp);
    if(count == m*n){
        for(int i = 0 ; i < path.size() ; i++)
            cout<<path[i];
        cout<<endl;
        return true;
    }
    int p , q;
    for(int i = 0 ; i < 8 ; i++){
        p = row + x[i] , q = col + y[i];
        if(valid(p , q) && !check[p][q]){
            check[p][q] = 1;
            if(dfs( path , p , q , count+1 , check)) return true;
            check[p][q] = 0;
        }
    }
    return false;
}
bool path(){
    for(int i = 0 ; i < n; i ++){
        for(int j = 0 ; j < m ; j++){
            int check[30][30];
            memset(check , 0 , sizeof check);
            check[i][j] = 1;
            vector<string> path;
            if(dfs(path , i , j ,1 , check )) return true;
        }
    }
    return false;
}
int main(){ 
     
    int t;
    cin>>t;
    while(t--){
        cin>>n>>m;
        if(!path()) cout<<-1<<endl;
    }
    
    return 0;
}

Sunday, August 9, 2015

HERDING-Herding

Herding

Given below c++ code is for HERDING spoj or Herding spoj.

Simple BFS with some backtracking. Also can be easily done with DFS.




/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 09 August 2015 (Sunday) 03:27
===================================================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define mod 1000000009
char opposite[200];
bool valid(int x , int y , int n , int m){
    if(x >=0 && y>=0 && y< m && x<n)
        return true;
    return false;
}
int main(){
    int n , m;
    scanf("%d%d",&n , &m);
    char arr[n+9][m+9];
    for(int i = 0 ; i < n ; i++)
        scanf("%s",arr[i]);
    int count = 0 , check[n+9][m+9];
    memset(check , 0 , sizeof check);
    int prev = 0 , color=0;
    for(int i = 0 ; i < n ; i++){
        for(int j = 0 ; j < m ; j++){
            queue<pii >q;
            q.push(mp(i , j));
            vector<pii > path;
            path.pb(mp(i , j));
            if(!check[i][j]){
                color = prev + 1;
                check[i][j] = color;
                while(!q.empty()){
                    pii pos = q.front();
                    q.pop();
                    int x = pos.first , y = pos.second;
                    if(arr[x][y] == 'S')
                        x++;
                    else if(arr[x][y] == 'E')
                        y++;
                    else if(arr[x][y] == 'N')
                        x--;
                    else if(arr[x][y] == 'W')
                        y--;
                    if(valid(x , y , n , m) && check[x][y] == 0){
                        path.pb(mp(x , y));
                        q.push(mp(x , y));
                        check[x][y] = color;
                    } else if(valid(x , y , n , m) && check[x][y]!=0){
                        int save = check[x][y];
                        if(check[x][y] < color){
                            for(int p = 0 ; p < path.size() ; p++)
                                check[path[p].first][path[p].second] = check[x][y];
                        }
                        color =save;
                        break;
                    }
                }
                if(color>prev)
                    prev++;
            }
        }
    }
    cout<<prev<<endl;
    return 0;
}

Thursday, February 12, 2015

BUGLIFE-A Bug’s Life

A Bug’s Life


Given below code is for BUGLIFE spoj or A Bug's life spoj.


Explanation : Use simple bfs to find cycle of odd length , or check graph is bipartite or not.


#include <bits/stdc++.h>
using namespace std;
int G[2019][2019]={0,0};
int chec(int a);
int n;
int colorArr[2019];
int main()
{
    int t,m,x,y,i;
    scanf("%d",&t);
    for(int test = 1 ; test <= t ; test++)
    {
        memset(G,0,sizeof(G));
        int flag=0;
        scanf("%d%d",&n,&m);
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            G[x][y]=1;
            G[y][x]=1;
        }
        int res;
        for ( i = 1; i <= n; i++)
        colorArr[i] = -1;

        for(i=1;i<=n;i++)
        {
            if(colorArr[i]== -1)
            {
                res=chec(i);
                if(res==0)
                {
                    flag=1;
                    break;
                }
            }
        }
        if(flag==1)
            printf("Scenario #%d:\nSuspicious bugs found!\n",test);
        else
            printf("Scenario #%d:\nNo suspicious bugs found!\n",test);
    }
    return 0;
}

int chec(int start)
{   
    colorArr[start] = 1;
 
    queue <int> q;
    q.push(start);
    while (!q.empty())
    {
       
        int i = q.front();
        q.pop();
 
       
        for (int j = 1; j <= n; j++)
        {
            if (G[i][j] && colorArr[j] == -1)
            {
       
                colorArr[j] = 1 - colorArr[i];
                q.push(j);
            }
            else if (G[i][j] && colorArr[j] == colorArr[i])
                return 0;
        }
    }
    return 1;
}

Sunday, February 8, 2015

ARBITRAG-Arbitrage

Arbitrage

Given below cpp code is for ARBITRAG spoj or arbitrage spoj.

Explanation :- simple Floyd-Warshall implementation.




#include <bits/stdc++.h>
using namespace std;
int main(){
    for(int test = 1; ; test++){
        int n;
        scanf("%d",&n);
        if(n == 0)
            return 0;
        map<string , int> m;
        string s , source , dest;
        for(int i = 0 ; i < n ; i++){
            cin>>s;
            m[s] = i;
        }
        int table;
        cin>>table;
        double dist[50][50] , cost ;
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < n ; j++){
                dist[i][j] = 0.0;
            } 
        }
        for(int i = 0 ; i < table ; i++){
            cin>>source>>cost>>dest;
            int x , y;
            x = m[source];
            y = m[dest];
            dist[x][y] = cost;
        }
        //floyd warshall
        for(int k = 0 ; k < n ; k++)
            for(int i = 0 ; i < n ; i++)
                for(int j = 0 ; j < n ; j++)
                        dist[i][j] = max(dist[i][j] , dist[i][k] * dist[k][j]);
        int flag = 0;
        for(int i = 0 ; i < n ; i++){
                if(dist[i][i] > 1.0 ){
                    flag = 1;
                    break;
                }
        }
        if(flag)
            cout<<"Case "<<test<<": Yes\n";
        else
            cout<<"Case "<<test<<": No\n";
    }
}

Monday, February 2, 2015

MLASERP-Laser Phones

Laser Phones

Given below code is for MLASERP spoj or laser phones spoj.

Explanation : - 

First find the positions of the cow where they are situated . Now make one of them as source and other one destination . Now start bfs from source an apply bfs in following manner .

1-> for each cell having row = source.row or column = source.column make distance =0 as you need no mirror  and push all that in queue.

2-> Next time whichever node you popped from queue , check if distance of all cell having row =poppedcell.row or column = poppedcell.column is greater then distance filled in poppedcell + 1 , if condition is true then push the node in queue .

Repeat above process till you reach the destination node.


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define pii pair<int , int >
bool check[109][109];
int main(){
    int n , m;
    cin>>m>>n;
    string s[n+9];
    for(int i = 0 ; i < n ; i++)
        cin>>s[i];

    pii source , dest;
    bool first = 1;
    for(int i = 0 ; i < n  ; i ++){
        for(int j = 0 ; j < m ; j++)
            if(s[i][j] == 'C'){
                if(first)
                    source = mp(i , j) , first = 0;
                else{
                    dest = mp(i , j);
                    break;
                }
            }
    }
    
    queue<pair<int , int > > q;
    q.push(source);
    int res = 0;
    int dist[109][109];
    for(int i = 0 ; i < 109 ; i++)
        for(int j = 0 ; j < 109 ; j++)
            dist[i][j] = INT_MAX;

    dist[source.first][source.second]=0;
    while(!q.empty()){
        int i , j ; 
        i = q.front().first;
        j = q.front().second;
        int u = i, v = j;
        while(u+1 < n && s[u+1][j]!='*'  ){
            if( dist[u+1][j] > dist[i][j] + 1){
                q.push(mp(u+1 , j));
                dist[u+1][j] = dist[i][j] +1;
            }
            u++;
        }
        u = i;
        while(u-1 >=0 && s[u-1][j]!='*'  ){
            if(dist[u-1][j] > dist[i][j] + 1){
                q.push(mp(u-1 , v) );
                dist[u-1][j] = dist[i][j] +1;
            }
            u--;
        }
        while(v+1 < m && s[i][v+1]!='*' ){
            if( dist[i][v+1] > dist[i][j] + 1){
                q.push(mp(i , v+1));
                dist[i][v+1] = dist[i][j] +1;
            }
            v++;
        }
        v = j;
        while(v-1 >= 0 && s[i][v-1]!='*'  ){
            if(dist[i][v-1] > dist[i][j] + 1){
                q.push(mp(i , v-1) );
                dist[i][v-1] = dist[i][j] + 1;
            }
            v--;
        }
        q.pop();
    }
    cout<<dist[dest.first][dest.second]-1<<endl;
    return 0;
}

Sunday, January 11, 2015

NAKANJ-Minimum Knight moves !!!

Minimum Knight moves !!!

Given below code is for nakanj spoj or minimum knight moves spoj.


Question is same as costly chess(CCHESS) problem of spoj.




#include <bits/stdc++.h>
using namespace std;
#define MP make_pair
int dist[100][100];
map<int , pair<int , int > > mp;
map<pair<int , int > , int > m;
void fill_distance(int v){
    int path[8][2];
    int x = mp[v].first , y = mp[v].second;
    path[0][0] = x+2 , path[0][1] = y+1;
    path[1][0] = x+2 , path[1][1] = y-1;
    path[2][0] = x-2 , path[2][1] = y+1;
    path[3][0] = x-2 , path[3][1] = y-1;
    path[4][0] = x+1 , path[4][1] = y+2;
    path[5][0] = x+1 , path[5][1] = y-2;
    path[6][0] = x-1 , path[6][1] = y+2;
    path[7][0] = x-1 , path[7][1] = y-2;

    for(int i = 0 ; i < 8 ; i ++)
    {
        if(path[i][0] >= 0 && path[i][0] < 8 && path[i][1] >= 0 && path[i][1] < 8){
            dist[v][m[MP(path[i][0] , path[i][1])]] = 1;
        }
    }
}
void pre(){
    int k = 0;
    for(int i = 0 ; i < 64 ; i++)
        for(int j = 0 ; j < 64 ; j++){
            dist[i][j] = 9999999;
            dist[j][j] = 0;
        }
    for(int i = 0 ; i<64 ; i++){
        fill_distance( i);
    }
    for(int k = 0 ; k < 64 ; k++)
        for(int i = 0 ; i < 64 ; i++)
            for(int j = 0 ; j < 64 ; j++)
                    dist[i][j] = min(dist[i][j] , dist[i][k] + dist[k][j]);
}
int main()
{
    int k = 0;
    for(int i = 0 ; i < 8 ; i++)
        for(int j = 0 ; j < 8 ; j++){
            mp[k] = MP(i , j);
            m[MP(i , j)] = k;
            k++;
        }
    pre();
    int t;
    scanf("%d ",&t);
    while(t--){
        char inp[10];
        gets(inp);
        int x, y , u ,v;
        x = inp[0] - 97;
        y = inp[1] - 49;
        u = inp[3] - 97;
        v = inp[4] - 49;
        int source = m[MP(x,y)], dest = m[MP(u,v)];
        printf("%d\n",dist[source][dest]) ;
    }
    return 0;
}

CCHESS-COSTLY CHESS

COSTLY CHESS

Given below code is for cchess spoj or costly chess spoj.



In this question I apply Floyd Warshall's algorithm to find the minimum cost between all pair of blocks of chess, and then for given input I just print out that value from table.




#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define MP make_pair
LL dist[100][100];
map<int , pair<int , int > > mp;
map<pair<int , int > , int > m;
void fill_distance(int v){
    int path[8][2];
    int x = mp[v].first , y = mp[v].second;
    path[0][0] = x+2 , path[0][1] = y+1;
    path[1][0] = x+2 , path[1][1] = y-1;
    path[2][0] = x-2 , path[2][1] = y+1;
    path[3][0] = x-2 , path[3][1] = y-1;
    path[4][0] = x+1 , path[4][1] = y+2;
    path[5][0] = x+1 , path[5][1] = y-2;
    path[6][0] = x-1 , path[6][1] = y+2;
    path[7][0] = x-1 , path[7][1] = y-2;

    for(int i = 0 ; i < 8 ; i ++)
    {
        if(path[i][0] >= 0 && path[i][0] < 8 && path[i][1] >= 0 && path[i][1] < 8){
            dist[v][m[MP(path[i][0] , path[i][1])]] = x*path[i][0] + y*path[i][1];
        }
    }
}
void pre(){
    int k = 0;
    for(int i = 0 ; i < 64 ; i++)
        for(int j = 0 ; j < 64 ; j++){
            dist[i][j] = INT_MAX;
            dist[j][j] = 0;
        }
    for(int i = 0 ; i<64 ; i++){
        fill_distance( i);
    }
    for(int k = 0 ; k < 64 ; k++)
        for(int i = 0 ; i < 64 ; i++)
            for(int j = 0 ; j < 64 ; j++)
                    dist[i][j] = min(dist[i][j] , dist[i][k] + dist[k][j]);
}
int main()
{
    int k = 0;
    for(int i = 0 ; i < 8 ; i++)
        for(int j = 0 ; j < 8 ; j++){
            mp[k] = MP(i , j);
            m[MP(i , j)] = k;
            k++;
        }
    pre();
    int x, y , u ,v;
    while(scanf("%d%d%d%d",&x , &y , &u ,&v)!=EOF){
        int source = m[MP(x,y)], dest = m[MP(u,v)];
        dist[source][dest] == INT_MAX ? printf("-1\n") : printf("%lld\n",dist[source][dest]) ;
    }
    return 0;
}

Tuesday, August 19, 2014

PPATH-Prime Path

 Prime Path

Below given code is for PPATH spoj or Prime path spoj.

Hint:- Use BFS and sieve.


#include <bits/stdc++.h>
using namespace std;
bool check[10009];
void sieve()
{
    for(int i=2;i<=100;i++)
    {
        if(!check[i])
        {
            for(int j=i*i;j<=10000;j+=i)
                check[j] = true;
        }
    }
}
int conv_num(int a[])
{
    int temp=0,k=0;
    while(k<4){
        temp = temp*10 + a[k];
        k++;
    }
    return temp;
}
void arr(int a[],int num)
{
    int w=3;
    while(num)
    {
        a[w--] = num%10;
        num/=10;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    sieve();
    while(t--)
    {
        int digit[4],dist[10009],a,b,parent[10009];
        scanf("%d%d",&a,&b);
        memset(dist,-1,sizeof(dist));
        memset(parent,-1,sizeof(parent));
        queue<int> q;
        dist[a]=0;
        q.push(a);
        parent[a]=0;
        while(!q.empty())
        {
            int num = q.front();
            for(int k=3;k>=0;k--)
            {
                arr(digit,num);
                for(int i=0;i<=9;i++)
                {
                    digit[k] = i;
                    int temp = conv_num(digit);
                    if((!check[temp]) && dist[temp]==-1 && temp>=1000)
                    {
                        dist[temp]= dist[num] + 1;
                        parent[temp]=num;
                        q.push(temp);
                    }
                }
            }
            q.pop();
        }
        dist[b]==-1 ? cout<<"Impossible"<<endl : cout<<dist[b]<<endl;
    }
    return 0;
}

PT07Z-Longest path in a tree

Longest path in a tree

Given below code is for PTZ07Z spoj or Longest path in a tree spoj.

You can solve this using DFS of applying BFS twice.

For BFS twice 
In first bfs you have to find maximum length of node from root then in second bfs consider that node as root and find maximum distance from that .That will be our answer.

1-> Using DFS.

#include <bits/stdc++.h>
using namespace std;
#define MAX 100009
bool check[MAX]={false};
int total=0;
int dfs(vector<int> v[],int root)
{
    int m,m1=-1,m2=-1;
    check[root]=1;
    for(int i=0;i<v[root].size();i++)
    {
        if(!check[v[root][i]]){
            m = dfs(v,v[root][i]);
            if(m>=m1)
            {
                m2= m1;
                m1 = m;
            }
            else if(m>m2)
                m2=m;
        }
    }
    total = max(total , m1+m2+2);
    return (m1 + 1);
}
int main()
{
    int n,a,b;
    cin>>n;
    vector<int> v[n+9];
    for(int i=0;i<n-1;i++){
        scanf("%d%d",&a,&b);
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(v,1);
    cout<<total<<endl;
}

1-> Using Double BFS .



#include <bits/stdc++.h>
using namespace std;
#define MAXN 10000
vector < int > g[MAXN + 1];
int maxWt[MAXN + 1];
bool check[MAXN + 1];
void bfs(int n)
{
    queue <pair<int, int> > q;
    q.push(make_pair(n, 0));
    while (!q.empty())
    {
        int root = q.front().first;
        int wt = q.front().second;
        check[root] = true;
        for(int i = 0; i<g[root].size(); i++)
        {
            if (!check[g[root][i]])
            {
                q.push(make_pair(g[root][i], wt + 1));
            }
        }
        maxWt[root] = wt;
        q.pop();
    }
}
int main()
{
    int n,a,b;
    cin>>n;
    vector<pair<int,int> > v[n+9];
    for(int i=0;i<n-1;i++)
    {
        cin>>a>>b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    bfs(1);
    int maxRoot = 0;
    for(int i = 1; i<= n; i++)
        maxRoot = maxWt[maxRoot] < maxWt[i] ? i : maxRoot; 
    memset(maxWt, 0, sizeof(maxWt));
    memset(check, 0, sizeof(check));
    bfs(maxRoot);
    maxRoot = 0;
    for(int i = 1; i<= n; i++)
        maxRoot = max(maxRoot, maxWt[i]);
    cout<<maxRoot<<endl;
    return 0;
}

Monday, August 18, 2014

PARADOX-Paradox

Paradox

Given below code is for PARADOX spoj.

Hint:- DFS
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while (1)
    {
        scanf("%d",&n);
        if (n==0)
            return  0;
        char type[10];
        int array[n+1],actual[n+1],flag=0;
        
        for (int i=1; i<n+1; i++)
        {
            scanf("%d %s",&array[i],type);
            if (type[0]=='f')
                actual[i]=0;
            else
                actual[i]=1;
        }
        for (int i=1; i<=n; i++)
        {
            bool ans[n+1],mark[n+1];
            memset(ans,0,sizeof(ans));
            memset(mark,0,sizeof(ans));
            bool save;
           // if (mark[i]==0)
            {
                mark[i]=1;
                ans[i]=1;
                int temp=array[i];
                bool temp1=ans[i];
                save=actual[i];
                while (!mark[temp])
                {
                    if (temp1==1)
                        ans[temp]=save;
                    else
                        ans[temp]=!save;

                    mark[temp]=1;
                    temp1=ans[temp];
                    save=actual[temp];
                    temp=array[temp];
                }
                if (temp1==1 && save!=ans[temp])
                    flag=1;
                else if(temp1==0 && save==ans[temp])
                    flag=1;
            }
            if (flag==1)
                break;
        }
        if (flag==0)
            printf("NOT PARADOX\n");
        else
            printf("PARADOX\n");
    }
}

Sunday, August 17, 2014

BUGLIFE-A Bug’s Lifeu

A Bug’s Life

Given below code is for BUGLIFE spoj or A Bug life spoj.

This question is simple implementation of BFS.


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t,k=0;
    scanf("%d",&t);
    while (t--)
    {
        k++;
        int n,m,flag=0;
        scanf("%d %d",&n,&m);
        vector<int> graph[n+1];
        for (int i=0; i<m; i++)
        {
            int a,b;
            scanf("%d %d",&a,&b);
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        int *list=(int*)calloc(n+1,sizeof(int));
        //memset(list,2,sizeof(list));
        for (int i=0; i<=n; i++)
            list[i]=2;
        int *mark=(int *)calloc(n+1,sizeof(int));
        for (int i=1; i<=n; i++)
        {
            if (mark[i]==0)
            {
                queue<int> q;
                q.push(i);
                list[i]=1;
                while (q.size()!=0)
                {
                    int temp=q.front();
                    for (int j=0; j<graph[temp].size(); j++)
                    {
                        if (list[graph[temp][j]]==2)
                            list[graph[temp][j]]=!list[temp];
                        else if (list[graph[temp][j]]==list[temp])
                        {
                            flag=1;
                            break;
                        }
                        if (mark[graph[temp][j]]==0)
                            q.push(graph[temp][j]);
                    }
                    q.pop();
                    mark[temp]=1;
                    if (flag==1)
                        break;
                }
            }
            if (flag==1)
                break;
        }
        /*for (int i=0; i<=n; i++)
            cout<<list[i]<<" ";
        cout<<endl;*/
        printf("Scenario #%d:\n",k);
        if (flag==1)
            printf("Suspicious bugs found!\n");
        else
            printf("No suspicious bugs found!\n");
    }
    return 0;
}