Cow Picnic

Problem Description

The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N (1 ≤ N ≤ 1,000) pastures, conveniently numbered 1...N. The pastures are connected by M (1 ≤ M ≤ 10,000) one-way paths (no path connects a pasture to itself).

The cows want to gather in the same pasture for their picnic, but (because of the one-way paths) some cows may only be able to get to some pastures. Help the cows out by figuring out how many pastures are reachable by all cows, and hence are possible picnic locations.

Input

Line 1: Three space-separated integers, respectively: <i>K</i>, <i>N</i>, and <i>M</i> <br>Lines 2..<i>K</i>+1: Line <i>i</i>+1 contains a single integer (1..<i>N</i>) which is the number of the pasture in which cow <i>i</i> is grazing. <br>Lines <i>K</i>+2..<i>M</i>+<i>K</i>+1: Each line contains two space-separated integers, respectively <i>A</i> and <i>B</i> (both 1..<i>N</i> and <i>A</i> != <i>B</i>), representing a one-way path from pasture <i>A</i> to pasture <i>B</i>.

Output

Line 1: The single integer that is the number of pastures that are reachable by all cows via the one-way paths.

扫描二维码关注公众号,回复: 2500512 查看本文章

Sample Input

 

2 4 4 2 3 1 2 1 4 2 3 3 4

Sample Output

 

2

bfs或Dfs

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define maxn 1005
using namespace std;
int head[maxn],cow[maxn],mark[maxn],vis[maxn];
int k,n,m,tot=0,cnt;
struct node
{int v,next;
}edge[10*maxn];
void init()
{cnt=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int main()
{
    scanf("%d%d%d",&k,&n,&m);
    init();
    for(int i=1;i<=k;i++)
    scanf("%d",&cow[i]);
    int x,y;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        add(x,y);
    }
    queue<int>q;
    for(int i=1;i<=k;i++)
    {memset(vis,0,sizeof(vis));
        q.push(cow[i]);
        vis[cow[i]]=1;
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            for(int i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].v;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                }
            }

    }
    for(int j=1;j<=n;j++)
    {if(vis[j])
        mark[j]++;
    }

}
for(int i=1;i<=n;i++)
if(mark[i]==k)
tot++;
printf("%d\n",tot);
return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
struct node
{
    int v,next;
}edge[10005];
int k,m,n,cnt,tot;
int head[2000];
int vis[2000];
int mark[2000];
int cow[2000];
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v)
{

    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void dfs(int u)
{
    vis[u]=1;
    mark[u]++;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
      int v=edge[i].v;
      if(!vis[v])
      dfs(v);

    }
}
int main()
{
    scanf("%d%d%d",&k,&n,&m);
    tot=0;
    init();
    int x,y;
    for(int i=1;i<=k;i++)
    scanf("%d",&cow[i]);

    for(int i=1;i<=m;i++)
    {scanf("%d%d",&x,&y);
    add(x,y);
    }
    for(int i=1;i<=k;i++)
    {
        memset(vis,0,sizeof(vis));
        dfs(cow[i]);
    }
    for(int i=1;i<=n;i++)
    if(mark[i]==k)

    tot++;
    printf("%d\n",tot);
    return 0;

}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/81235084