10608 Why RE??

Write here if you have problems with your C source code

Moderator: Board moderators

10608 Why RE??

Postby cse.mehedi » Tue Apr 17, 2012 4:08 pm

I can not understand why RE!
Please Any One Help Me! :(
Code: Select all

#include<stdio.h>
#define size 31000

bool adj_mat[size][size],visited[size];

 main()
{
   
    long n,node,con_no,a,b,i,dequeue,counter,tp,friends;
    scanf("%ld",&n);
    while(n--)
    {
        scanf("%ld %ld",&node,&con_no);
        if(con_no==0)
            printf("0\n");
        else
        {
        adj_mat[size][size]={0},visited[size]={0};
        long arr[size]={0};
        while(con_no>=1)
        {
            scanf("%ld %ld",&a,&b);
            if(adj_mat[b][a]==0)
            adj_mat[a][b]=1;
            con_no--;
        }
        friends=0;
        for(int k=1;k<=node;k++)
        {
            i=1,arr[1]=k,counter=1,tp=1;
            while(1)
            {
                dequeue=arr[i];
                if(dequeue==0) break;
                if(visited[i]==0)
                {
                    for(int j=1;j<=node;j++)
                    {
                        if(adj_mat[dequeue][j]==1)
                        {
                            counter++;
                            visited[j]=1;
                            arr[tp++]=j;
                        }
                    }
                    visited[i]=0;
                }
                if(friends<counter) friends=counter;
                i++;
            }
        }
       printf("%ld\n",friends); }
    }
    return 0;
}


Last edited by cse.mehedi on Fri May 25, 2012 2:09 pm, edited 6 times in total.
cse.mehedi
New poster
 
Posts: 36
Joined: Sun Mar 18, 2012 8:18 am

Re: 10608 Why RE??

Postby brianfry713 » Tue Apr 17, 2012 10:43 pm

if you declare a_mat of size c then you can only access up to c-1.

You're getting a seg fault during the sample input on line 16: a_mat[i][j]=0;
brianfry713
Guru
 
Posts: 1751
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10608 Why RE??

Postby cse.mehedi » Tue Apr 17, 2012 10:53 pm

brianfry713 wrote:if you declare a_mat of size c then you can only access up to c-1.

You're getting a seg fault during the sample input on line 16: a_mat[i][j]=0;

I could not understand what should i do here "a_mat[i][j]=0". :(
cse.mehedi
New poster
 
Posts: 36
Joined: Sun Mar 18, 2012 8:18 am

Re: 10608 Why RE??

Postby brianfry713 » Wed Apr 18, 2012 11:51 pm

You fixed the issue you were having before, trying to access beyond the limit of the array. Now I think you might be getting RE because you are trying put too much memory on the stack. Instead of declaring a huge local array long a_mat[c+1][c+1] where c can be up to 30000, and the values are only 0 or 1, try using a global bool array in c++.

This input causes your code to seg fault on my machine:
Code: Select all
1
30000 0
brianfry713
Guru
 
Posts: 1751
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10608 Why RE??

Postby cse.mehedi » Thu Apr 19, 2012 7:56 pm

brianfry713 wrote:You fixed the issue you were having before, trying to access beyond the limit of the array. Now I think you might be getting RE because you are trying put too much memory on the stack. Instead of declaring a huge local array long a_mat[c+1][c+1] where c can be up to 30000, and the values are only 0 or 1, try using a global bool array in c++.

This input causes your code to seg fault on my machine:
Code: Select all
1
30000 0

Getting WA!
cse.mehedi
New poster
 
Posts: 36
Joined: Sun Mar 18, 2012 8:18 am

Re: 10608 Why RE??

Postby brianfry713 » Thu Apr 19, 2012 11:03 pm

Input:
Code: Select all
1
2 1
1 2


AC Output:
Code: Select all
2
brianfry713
Guru
 
Posts: 1751
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10608 Why RE??

Postby cse.mehedi » Fri Apr 20, 2012 1:02 pm

TLE!!!!!
cse.mehedi
New poster
 
Posts: 36
Joined: Sun Mar 18, 2012 8:18 am

Re: 10608 Why RE??

Postby brianfry713 » Fri Apr 20, 2012 8:56 pm

Input:
Code: Select all
2
2 1
1 2
2 1
1 2


AC output:
Code: Select all
2
2
brianfry713
Guru
 
Posts: 1751
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10608 Why RE??

Postby cse.mehedi » Tue May 15, 2012 9:28 pm

brianfry713 wrote:Input:
Code: Select all
2
2 1
1 2
2 1
1 2


AC output:
Code: Select all
2
2

TLE!!!!!!!!!
cse.mehedi
New poster
 
Posts: 36
Joined: Sun Mar 18, 2012 8:18 am

Re: 10608 Why RE??

Postby brianfry713 » Tue May 15, 2012 10:52 pm

Input:
Code: Select all
2
2 1
1 2
3 1
2 3


AC Output:
Code: Select all
2
2
brianfry713
Guru
 
Posts: 1751
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10608 Why RE??

Postby cse.mehedi » Fri May 25, 2012 2:08 pm

I modified my code but i am getting RE :oops: :oops: :oops:
cse.mehedi
New poster
 
Posts: 36
Joined: Sun Mar 18, 2012 8:18 am

Re: 10608 Why RE??

Postby brianfry713 » Fri Jun 01, 2012 1:54 am

This won't compile on my system.
You have these globals defined:
bool adj_mat[size][size],visited[size];
This isn't a declaration:
adj_mat[size][size]={0},visited[size]={0];
You can't access beyond adj_mat[size-1][size-1].
To clear an array using ={0} you must do it at declaration. You could try using memset() instead.
brianfry713
Guru
 
Posts: 1751
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA


Return to C

Who is online

Users browsing this forum: No registered users and 1 guest