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 1340502116 at 2016-06-29 19:45:51 on Problem 3256
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=1005;
bool mp[MAXN][MAXN];
int mark[MAXN];
int unite[MAXN];
int k,n,m;
int vis[MAXN];
void dfs(int u,int s)
{
	vis[u]=1;
	unite[u]+=s;
	for(int i=1;i<=n;i++)
	{
		if(mp[u][i]&&!vis[i])
		{
			dfs(i,s);
		}
	}
}
int main()
{
	while(scanf("%d%d%d",&k,&n,&m)!=EOF)
	{
		memset(mp,false,sizeof(mp));
		memset(mark,0,sizeof(mark));
		memset(unite,0,sizeof(unite));
		for(int i=0;i<k;i++)
		{
			int x;
			scanf("%d",&x);
			mark[x]++;
		}
		for(int i=0;i<m;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			mp[u][v]=true;
		}
		int res=0;
		for(int i=1;i<=n;i++)
		{
			if(mark[i])
			{
				memset(vis,0,sizeof(vis));
				dfs(i,mark[i]);
			}
		}
		for(int i=1;i<=n;i++)
			if(unite[i]==k)
				res++;
		printf("%d\n",res);
	}
	return 0;
}

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