PAT 1013

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The global variable n cannot directly in the function DFS () in use, and this:

Not the same, other completely copy his

#include <stdio.h>
#include <string.h>

int map[1001][1001];
int visited[1001];
int i;
int n,m,k;


void dfs(int v,int max){//The separate definition of Max, not with the global variable n, or read n is 0!!!
	visited[v] = 1;
	int j;
	for(j=1;j<=max;j++){
		if(visited[j]==0 && map[j][v]==1){
			dfs(j,max);
		}
	}
}

int main(){
	//freopen("in.txt","r",stdin);
	
	int j;
	for(i=0;i<1001;i++){
		for(j=0;j<1001;j++){
			map[i][j] = 0;
		}
	}

	int n,m,k;
	scanf("%d %d %d",&n,&m,&k);
	while(0 < m--){
		int a,b;
		scanf("%d %d",&a,&b);
		map[a][b] = 1;
		map[b][a] = 1;		
	}

	int num;
	while(0 < k--){
		num = 0;
		int temp;
		scanf("%d",&temp);

		memset(visited,0,sizeof(visited));
		visited[temp] = 1;//This point to any other point of the path are blocked

		for(i=1;i<=n;i++){			
			if(visited[i]==0){	
				dfs(i,n);
				num ++;
			}
		}
		printf("%d\n",num-1);
	}
	
	return 0;
}


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Leander at February 24, 2014 - 3:44 PM