Can anyone tell me .. what's wrong with this code?Why it's giving Runtime Error?

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define vi vector<int>
#define vii vector<vector<int>>
#define vl vector<long long>
#define vf vector<float>
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)

const int N=1e6+9;
vii vis(N);

void dfs(vii& graph,int i,int j,int& count){
     if(!graph[i][j]) return;
     if(vis[i][j]) return;
     vis[i][j]=1;
     count+=4;
     if(vis[i+1][j]){
        count-=2;
     }
     if(vis[i][j+1]){
         count-=2;
     }
     if(vis[i-1][j]){
         count-=2;
     }
     if(vis[i][j-1]){
         count-=2;
     }

     dfs(graph,i+1,j,count);
     dfs(graph,i,j+1,count);
     dfs(graph,i-1,j,count);
     dfs(graph,i,j-1,count);

}

int main(){
    fast;
    int r,c,n;
    cin>>r>>c>>n;
    vii graph(r+2,vi (c+2,0));
    while(n--){
        int x,y;cin>>x>>y;
        graph[x][y]=1;
    }

    int ans=0;
    for(int i=1;i<=r;i++){
        for (int j=1; j<=c;j++){
            if(graph[i][j]==1){
                if(vis[i][j]) continue;
                int count=0;
                dfs(graph,i,j,count);
                ans=max(ans,count);
                count=0;
            }
        }
    }
    cout<<ans;
    return 0;
}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Also: what input is giving the Runtime Error?

@ssjgz I am getting Runtime Error in all the inputs.Anything…

Please give an example input :slight_smile:

One thing leaps out - you’re checking vis[i][j] all over the place, but I can’t see you assigning it a value anywhere.

2 Likes

Still giving Runtime Error.

Input:
4 4 9
1 4
2 1
2 2
2 3
4 3
4 1
4 2
3 1
3 3

Post your latest code XD