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 |
O(k*m) 洪水,dfs遍历 一下。#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int N=1e3+9; const int M=1e5+9; int k,n,m; int box[N],kk=0; int a[109],vis[N],v[N]; struct node{int t,next;}e[M]; void add(int s,int t) { e[kk].t=t,e[kk].next=box[s];box[s]=kk++; } void dfs(int now) { v[now]=1; for(int i=box[now];~i;i=e[i].next) if(!v[e[i].t]) dfs(e[i].t); } void init() { scanf("%d%d%d",&k,&n,&m); for(int i=1;i<=k;i++) scanf("%d",&a[i]); memset(box,-1,sizeof(box)); while( m--) { int s,t;scanf("%d%d",&s,&t); add(s,t); } for(int i=1;i<=k;i++) { memset(v,0,sizeof(v)); dfs(a[i]); for(int j=1;j<=n;j++) vis[j]+=v[j]; } int ans=0; for(int i=1;i<=n;i++) if(vis[i]==k) ans++; cout<<ans<<endl; } int main() { freopen("in.txt","r",stdin); init(); // cout << "Hello world!" << endl; return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator