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

O(k*m) 洪水,dfs遍历 一下。

Posted by WilliamACM at 2013-01-21 18:02:38 on Problem 3256
#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:
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