P2853 [USACO06DEC]牛的野餐Cow Picnic

题目描述

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.

K(1≤K≤100)只奶牛分散在N(1≤N≤1000)个牧场.现在她们要集中起来进餐.牧场之间有M(1≤M≤10000)条有向路连接,而且不存在起点和终点相同的有向路.她们进餐的地点必须是所有奶牛都可到达的地方.那么,有多少这样的牧场呢?

输入输出格式

输入格式:

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.

输出格式:

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

输入输出样例

输入样例#1:

2 4 4
2
3
1 2
1 4
2 3
3 4
输出样例#1:

2

我可怜的dfs、、、50分,剩下的点全T了、、
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 11000
using namespace std;
bool vis[N],vist[N];
int k,n,m,x,y,ans,tot,a[N],sum[N],head[N];
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
struct Edge
{
    int to,next,from;
}edge[N];
int add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
void dfs(int x)
{
    if(vis[x]) return ;
    vis[x]=true;vist[x]=true;
    for(int i=head[x];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(!vis[to]) dfs(to);
    }
    vis[x]=false;
}
int main()
{
    k=read(),n=read(),m=read();
    ;i<=k;i++) a[i]=read();
    ;i<=m;i++)
     x=read(),y=read(),add(x,y);
    ;i<=k;i++)
    {
        memset(vist,,sizeof(vist));
        dfs(a[i]);
        ;i<=n;i++)
         if(vist[i]) sum[i]++;
     }
    ;i<=n;i++)
     if(sum[i]==k) ans++;
    printf("%d",ans);
    ;
}

dfs

转bfs

我们枚举每一个牛的起始点,用bfs拓展一下它都能到哪里,然后让这个点的计数加1,

最后看一下都有多少点的计数是k就好了嘛。

这样看起来好像跟最短路的思路差不多,不过不用反向建边,我觉得应该更好想吧,而且拓展可达点这种问题不应该就用bfs而不是dfs吗

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 11000
using namespace std;
queue<int>q;
bool vis[N];
int k,n,m,x,y,ans,tot,a[N],sum[N],head[N];
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
struct Edge
{
    int to,next,from;
}edge[N];
int add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
int main()
{
    k=read(),n=read(),m=read();
    ;i<=k;i++) a[i]=read();
    ;i<=m;i++)
     x=read(),y=read(),add(x,y);
    ;i<=k;i++)
    {
        memset(vis,,sizeof(vis));
        q.push(a[i]);vis[a[i]]=true;
        while(!q.empty())
        {
            int x=q.front();q.pop();
            for(int i=head[x];i;i=edge[i].next)
            {
                int to=edge[i].to;
                if(!vis[to])
                {
                    vis[to]=true;
                    q.push(to);
                }
            }
        }
        ;i<=n;i++)
         if(vis[i]) sum[i]++;
    }
    ;i<=n;i++)
     if(sum[i]==k) ans++;
    printf("%d",ans);
    ;
}

AC的bfs

洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic的更多相关文章

  1. 洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

  2. 洛谷P2853 [USACO06DEC]牛的野餐Cow Picnic

    题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N ...

  3. bzoj1648 / P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 你愿意的话,可以写dj. 然鹅,对一个缺时间的退役选手来说,暴力模拟是一个不错的选择. 让每个奶牛都把图走一遍,显然那些被每个奶牛都走 ...

  4. P2853 [USACO06DEC]牛的野餐Cow Picnic

    ------------------------- 长时间不写代码了,从学校中抽身出来真的不容易啊 ------------------------ 链接:Miku ----------------- ...

  5. 题解【洛谷P2853】[USACO06DEC]牛的野餐Cow Picnic

    题目描述 The cows are having a picnic! Each of Farmer John's \(K (1 ≤ K ≤ 100)\) cows is grazing in one ...

  6. [USACO06DEC]牛的野餐Cow Picnic DFS

    题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N ...

  7. 洛谷P2854 [USACO06DEC]牛的过山车Cow Roller Coaster

    P2854 [USACO06DEC]牛的过山车Cow Roller Coaster 题目描述 The cows are building a roller coaster! They want you ...

  8. 洛谷P3080 [USACO13MAR]牛跑The Cow Run

    P3080 [USACO13MAR]牛跑The Cow Run 题目描述 Farmer John has forgotten to repair a hole in the fence on his ...

  9. 洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup

    https://www.luogu.org/problem/show?pid=3029 题目描述 Farmer John has hired a professional photographer t ...

随机推荐

  1. 【linux】【安全】服务器安全建议

    引用自 <鸟哥的linux私房菜-服务器篇>  http://cn.linux.vbird.org/linux_server/0210network-secure_1.php 建立完善的登 ...

  2. LeetCode(260) Single Number III

    题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...

  3. 【HIHOCODER 1055】 刷油漆(树上背包)

    描述 小Ho有着一棵灰常好玩的树玩具!这棵树玩具是由N个小球和N-1根木棍拼凑而成,这N个小球都被小Ho标上了不同的数字,并且这些数字都是处于1..N的范围之内,每根木棍都连接着两个不同的小球,并且保 ...

  4. hdu 5459

    Problem Description I've sent Fang Fang around 201314 text messages in almost 5 years. Why can't she ...

  5. Linux任务计划、周期性任务执行

    Linux任务计划.周期性任务执行 周期性任务执行: cron 守护进程(crond):服务,不间断地运行于后台 # service crond {start|stop|status|restart} ...

  6. HDU 5468 Puzzled Elena 莫比乌斯反演

    题意: 给出一棵树,每个点上有权值.然后求每棵子树中与根节点互质( \(gcd(a, b) = 1\) )的节点个数. 分析: 对于一颗子树来说,设根节点的权值为\(u\), \(count_i\)表 ...

  7. xcode各个版本下载 xcode7 xcode6 xcode5

    登录开发者帐号,选择 support,然后操作如下图: 登录开发者帐号,选择 support,然后操作如下图: 登录开发者帐号,选择 support,然后操作如下图: 登录开发者帐号,选择 suppo ...

  8. 一个Work Stealing Pool线程池的实现

    一.一般来说实现一个线程池主要包括以下几个组成部分: 1)线程管理器 :用于创建并管理线程池 . 2)工作线程 :线程池中实际执行任务的线程 . 在初始化线程时会预先创建好固定数目的线程在池中 ,这些 ...

  9. c++ 字符处理

    C++字符处理首选使用 string ,理由是string类型有很多简单易用的函数,不像char, char[], char* 等这些字符这么复杂. 如何用printf函数输出百分号? 打两个%%.. ...

  10. c中#与##的应用思考

    c中#与##的应用思考 原创 2014年02月25日 22:01:35 927 一. 思考出处 在读<<linux 0.12完全剖析>>初始化部分, init进程是通过fork ...