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. LeetCode(134) Gas Station

    题目 There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...

  2. POJ - 2250 Compromise (LCS打印序列)

    题意:给你两个单词序列,求出他们的最长公共子序列. 多组数据输入,单词序列长度<=100,单词长度<=30 因为所有组成LCS的单词都是通过 a[i] == b[j] 更新的. 打印序列的 ...

  3. 在ionic项目中使用极光推送实现推送 & 服务器端代码

    ionic start -a jPushDemo -i com.lawxin.fengkong jpushdemo blank meteor add cordova:cn.jpush.phonegap ...

  4. BZOJ 5441: [Ceoi2018]Cloud computing

    背包 #include<cstdio> #include<algorithm> using namespace std; int n,m,Len; long long F[2] ...

  5. luogu2577 [ZJOI2005]午餐

    dp[i]表示第一队打饭时间i的最优解 #include <algorithm> #include <iostream> #include <cstring> #i ...

  6. 和为n连续正数序列 【微软面试100题 第五十一题】

    题目要求: 输入一个正数n,输出所有和为n连续正数序列(至少两个). 例如输入15,由于1+2+3+4+5 = 4+5+6 = 7+8 = 15.所以输出3个连续序列1~5,4~6,7~8. 参考资料 ...

  7. webdriver高级应用- 改变一个页面对象的属性值

    适用于一些无法操作的元素,可以直接改他的属性从而操作,代码如下: #encoding=utf-8 from selenium import webdriver import unittest impo ...

  8. mysql primary partition分区

    尝试把数据库一个表分区 ALTER TABLE user PARTITION BY RANGE(TO_DAYS(`date`)) ( PARTITION p1004 VALUES LESS THAN  ...

  9. python学习-- class 类中需要注意的地方

    from django.db import models class Person(models.Model):     name = models.CharField(max_length=30) ...

  10. ccna 闫辉单臂路由 和 acl access control list

    ccna 闫辉单臂路由 和  acl   access control list 一单臂路由     当前园区网设计很少用到       成本低  小型的.局域网可用         二ACL acc ...