Cow Picnic(POJ-3256)

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: K, N, and M 

Lines 2..K+1: Line i+1 contains a single integer (1..N) which is the number of the pasture in which cow i is grazing. 

Lines K+2..M+K+1: Each line contains two space-separated integers, respectively A and B (both 1..N and A != B), representing a one-way path from pasture A to pasture B.

Output

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

Sample Input

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

Sample Output

2

题意:k 头牛,n 个牧场,m 条单向的路,求所有牛能到达的牧场数

思路:对每头牛进行 dfs 搜索,最后统计每个农场到达的牛数。

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 10001
#define MOD 123
#define E 1e-6
using namespace std;
int k,n,m;
int cow[N];
int sum[N];
int vis[N];
vector<int> g[N];
void dfs(int i)
{
    vis[i]=1;//标记为已到达
    int len=g[i].size();//从当前牧场可到达其他牧场的个数
    for(int j=0;j<len;j++)//枚举所有可到达的牧场
    {
        int next=g[i][j];
        if(!vis[next])//如果下一个牧场未到达,继续向下搜索
            dfs(next);
    }
}
int main()
{
    scanf("%d%d%d",&k,&n,&m);
    for(int i=1;i<=k;i++)
        scanf("%d",&cow[i]);
    while(m--)//vector邻接表建图
    {
        int x,y;
        scanf("%d%d",&x,&y);
        g[x].push_back(y);
    }

    for(int i=1;i<=k;i++)
    {
        dfs(cow[i]);//dfs每一头牛
        for(int j=1;j<=n;j++)//统计每一个牧场的到达数
        {
            sum[j]+=vis[j];
            vis[j]=0;
        }
    }

    int cnt=0;
    for(int i=1;i<=n;i++)//枚举所有牧场
        if(sum[i]==k)//如果该牧场所有牛均能到达
            cnt++;

    printf("%d\n",cnt);

    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Possible English version: Designing an Overseas Travel Plan Step 1: Choose your destination As a travel enthusiast, I have always dreamt of visiting the charming city of Paris, France. Known for its iconic landmarks, cultural heritage, and romantic atmosphere, Paris offers a perfect blend of history, art, and lifestyle that I would love to explore. Step 2: Collect basic information about the trip To plan my Parisian adventure, I need to gather some essential details, such as: - Travel time: I would prefer to go in the spring or fall, when the weather is mild and the crowds are manageable. - Travel mode: I could either fly directly from my hometown to Paris or take a connecting flight from a nearby city. Alternatively, I could take a train or a bus from another European city to reach Paris. - Accommodation: I would like to stay in a comfortable yet affordable hotel or hostel in a central location, so that I can easily access the main attractions and enjoy the local ambiance. - Budget: I need to estimate the total cost of the trip, including transportation, lodging, food, tickets, souvenirs, and miscellaneous expenses. I also need to allocate some extra money for unexpected events or emergencies. Step 3: Organize and analyze the plan Based on the above information, I can create a rough itinerary for my Paris trip, which might look like this: Day 1: Arrival in Paris, check-in at the hotel, stroll around the neighborhood, have a French dinner Day 2: Visit the Eiffel Tower, the Champs-Elysées, the Arc de Triomphe, and the Tuileries Garden Day 3: Explore the Louvre Museum, the Notre-Dame Cathedral, the Sainte-Chapelle, and the Latin Quarter Day 4: Take a day trip to Versailles Palace and Gardens, enjoy a picnic or a bike ride Day 5: Discover the Montmartre neighborhood, the Sacré-Cœur Basilica, and the Moulin Rouge cabaret Day 6: Have a leisurely brunch, shop for souvenirs or fashion items, attend a concert or a theater show Day 7: Departure from Paris, return home or continue the journey to other destinations By following this plan, I can savor the best of Paris in a week, without feeling rushed or bored. Of course, I can always adjust or customize the plan according to my interests, preferences, and availability. For example, I might skip some of the tourist traps and seek out hidden gems, or I might join a guided tour or a local event to enhance my cultural immersion. Whatever I do, I know that my Paris trip will be a memorable and enriching experience that I will cherish for a lifetime.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值