i am attempting to solve http://acm.uva.es/p/v1/125.html
i am giving my code below :
the feedback is wrong answer..
i am unable to spot the error..
pls help me..
thanx in advance !
#include<stdio.h>
#include<stdlib.h>
#define G 1
#define W 0
int func(int i,int j,int **graph,int **out,int *visited,int max);
void init(int **arr);
void reset(int *visited);
int main()
{
int noi,k,max=0,a,b,i,j,ret,count;
int *graph[30],visited[30],*out[30];
for(k=0;k<30;k++)
{
graph[k]=(int *)malloc(sizeof(int)*30);
out[k]=(int *)malloc(sizeof(int)*30);
}
for(count=0;scanf("%d",&noi)!=EOF;count++)
{
ret=0;
max=0;
init(graph);
init(out);
for(k=0;k<noi;k++)
{
scanf("%d",&a);
scanf("%d",&b);
graph[a][b]=1;
if(a>max)
max=a;
if(b>max)
max=b;
}
for(i=0;i<=max;i++)
{
reset(visited);
for(j=0;j<=max;j++)
{
if(visited[j]==W&&j!=i)
{
visited[j]=G;
for(k=0;k<=max;k++)
{
if(graph[j]
[k]==1&&visited[k]==W)
{
ret=func(i,k,graph,out,visited,max);
out[j][i]+=ret;
}
else if(graph[j]
[k]==1&&visited[k]==G)
{
out[j][i]+=out[k][i];
}
}
}
}
}
for(i=max;i>=0;i--)
{
for(j=max-i;j<=max;j++)
{
if(out[max-i][j]>0&&out[j][max-
i]>0)
{
out[max-i][j]=-1;
out[j][max-1]=-1;
out[max-i][max-i]=-1;
out[j][j]=-1;
}
}
}
for(i=0;i<=max;i++)
{
if(out[i][i]==-1)
{
for(j=0;j<=max;j++)
{
if((i!=j)&&(out[i][j]>0))
out[i][j]=-1;
}
}
}
printf("matrix for city %d\n",count);
for(i=0;i<=max;i++)
{
for(j=0;j<=max;j++)
{
printf("%d ",out[i][j]);
}
printf("\n");
}
}
}
int func(int i,int j,int **graph,int **out,int *visited,int max)
{
int ret=0;
int k;
if(j==i)
return(1);
else
{
visited[j]=G;
for(k=0;k<=max;k++)
{
if(graph[j][k]==1)
{
if(visited[k]==W)
{
ret+=func(i,k,graph,out,visited,max);
out[j][i]=ret;
}
else if(visited[k]==G)
{
out[j][i]+=out[k][i];
ret+=out[k][i];
}
}
}
return(ret);
}
}
void init(int **arr)
{
int i,j;
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
arr[i][j]=0;
}
}
}
void reset(int *visited)
{
int i;
for(i=0;i<30;i++)
{
visited[i]=W;
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---