677 - All Walks of length "n" from the first node

All about problems in Volume 6. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

sabir
New poster
Posts: 13
Joined: Thu Jun 14, 2007 8:48 am

Post by sabir »

Code modefied.But still WA.

Code: Select all

#include <stdio.h>
#include <math.h>

#define sz 20

long par[sz];
long n,vertex,count;
long mat[sz][sz];
long visit[sz],ans[sz];
bool flag;

void DfsVisit(long node)
{
	long i;

	visit[node]=1;
	
	if(n+1==count)
	{
		printf("(1,");
		flag=false;
		for(i=1;i<=n;i++)
		{
			printf("%ld", ans[i]);
			if(i<n)
			{
				printf(",");
			}
		}
		printf(")\n");
		return;
	}

	for(i=1;i<=vertex;i++)
	{
		if(mat[node][i]==1)
		{
			if(visit[i]==0)
			{
				ans[count]=i;
				count++;
				DfsVisit(i);
				visit[i]=0;
				count--;
			}
		}
	}
	return;
}

void Dfs()
{
	long i;

	for(i=1;i<=vertex;i++)
	{	
		par[i]=0;
		visit[i]=0;
	}

	DfsVisit(1);
}

int main()
{
	freopen("677.txt", "w", stdout);

	long i,j;
	bool tag=false;

	while(scanf("%ld %ld", &vertex, &n)==2)
	{
		flag=true;
		if(tag==true)
		{
			printf("\n");
		}

		tag=true;

		count=1;
		for(i=1;i<=vertex;i++)
		{
			for(j=1;j<=vertex;j++)
			{
				mat[i][j]=0;
			}
		}

		for(i=1;i<=vertex;i++)
		{
			for(j=1;j<=vertex;j++)
			{
				scanf("%ld", &mat[i][j]);
			}
		}

		Dfs();

		if(flag==true)
		{
			printf("no walk of length n\n");
		}
		if(scanf("%ld", &i)!=1)
		{
			break;
		}
	}
	return 0;
}

turcse143
Learning poster
Posts: 81
Joined: Wed May 09, 2007 9:59 pm
Location: (CSE,DU) Dhaka,Bangladesh

Re: 677 Why WA??? please help me

Post by turcse143 »

i think there is problem in this section.

Code: Select all

 
         if(visit[i]==0)
         {
            ans[count]=i;
            count++;
            DfsVisit(i);
            visit[i]=0;
            count--;
         }
i think visit=0 when the loop is complete & u return to the previous node.
''I want to be most laziest person in the world''

Mukit Chowdhury
Learning poster
Posts: 99
Joined: Fri Aug 17, 2012 9:23 pm
Location: Dhaka
Contact:

Re: 677 - All Walks of length "n" from the first node

Post by Mukit Chowdhury »

Getting WA !!! Please help !!!

Code: Select all

while(1)
{
printf("Accepted....... :D\n");
}

Post Reply

Return to “Volume 6 (600-699)”