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

最新文章

  1. [Unreal]学习笔记001
  2. 2007Hanoi双塔问题
  3. python学习第二天第二部分
  4. Android Framework------之Input子系统
  5. bmp to jpg
  6. JAVA实现前几秒几分钟几天前几年源码
  7. 如何让MyEclispe中英文切换
  8. UVA122-Trees on the level(链二叉树)
  9. 浅谈 PHP 变量可用字符
  10. 开源 自由 java CMS - FreeCMS2.0 签字
  11. Linq第一讲
  12. Java基础学习(三)&mdash;面向对象(上)
  13. 分享:SringBuffer与String的区别
  14. jQuery实现web页面固定列表搜索
  15. python之 文件读与写
  16. Beta 冲刺(7/7)
  17. increment/decrement/dereference操作符
  18. 网络爬虫re模块的findall()函数
  19. Java基础--对象的克隆
  20. redis 性能建议

热门文章

  1. NoSQL 数据库之MongoDB
  2. apicloud入门学习笔记1:简单介绍
  3. (转)iOS平台UDID方案比较
  4. Educational Codeforces Round 53 (Rated for Div. 2) C Vasya and Robot 二分
  5. poj 3176 三角数和最大问题 dp算法
  6. java处理excel
  7. js 常用判断
  8. 国内外移动端web适配屏幕方案总结
  9. jade和ejs两者的特点
  10. 静态方法,Arrays类,二维数组