Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
dfs#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator