Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

DFS嘛,又错哪了??好心人看一下!

Posted by dexter at 2004-04-10 16:49:31 on Problem 1295
#include<stdio.h>
#define MAX 20
struct SPAM
{
	int p;
	int T1,T2;
	char A1[20];
	char A2[20];
	char A3[20];
}msg[100];
int nmsg;
int matrix[MAX+1][MAX+1];
int visited[MAX+1];
int N;
char name[21][21];//begin from 1;
int init()
{
	int i,j,t;
	scanf("%d",&N);
	if(N==0)
		return 0;
	for(i=1;i<=N;i++)
		for(j=1;j<=N;j++)
			matrix[i][j]=0;
	for(i=1;i<=N;i++)
	{
		while(1)
		{
			scanf("%d",&t);
			if(t==0)
				break;
			matrix[i][t]=1;
		}
	}
	for(i=0;;i++)
	{
		scanf("%d",&t);
		if(t==0)
			break;
		msg[i].p=t;
		scanf("%d %d %s %s %s",&msg[i].T1,&msg[i].T2,msg[i].A1,msg[i].A2,msg[i].A3);
	}
	nmsg=i;
	for(i=1;i<=N;i++)
		scanf("%s",name[i]);
	return 1;
}
int count(int p)
{
	int i,j;
	i=0;
	for(j=1;j<=N;j++)
		if(matrix[p][j])
			i++;
	return i;
}
int firstadjvex(int u)
{
	int j;
	for(j=1;j<=MAX;j++)
		if(matrix[u][j])
			return j;
	return -1;
}
int nextadjvex(int u,int w)
{
	int j;
	for(j=w+1;j<=MAX;j++)
		if(matrix[u][j])
			return j;
	return -1;
}
void DFS(int v)
{
	int w;
	visited[v]=1;
	for(w=firstadjvex(v);w>=0;w=nextadjvex(v,w))
		if(!visited[w])
			DFS(w);
}
void  process(int num,int p)
{
	int temp,i;
	for(i=1;i<=N;i++)
		visited[i]=0;
	DFS(msg[num].p);
	if(visited[p]==0)
		temp=0;
	else
		temp=count(p);
	if(temp<msg[num].T1)
		printf("%s ",msg[num].A1);
	else if(temp>=msg[num].T1&&temp<msg[num].T2)
		printf("%s ",msg[num].A2);
	else printf("%s ",msg[num].A3);
}
main()
{
	int k,l;
	while(init())
	{
		for(k=1;k<=N;k++)
		{
			printf("%s: ",name[k]);
			for(l=0;l<=nmsg;l++)
				process(l,k);
			printf("\n");
		}
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator