• BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐


    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.

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

    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.

    第1行输入K,N,M.接下来K行,每行一个整数表示一只奶牛所在的牧场编号.接下来M行,每行两个整数,表示一条有向路的起点和终点

    Output

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

    所有奶牛都可到达的牧场个数

    题解:

    定义f[i][j],从i牛开始是否能到j这个点。

    用dfs或bfs模拟每个点,然后对于点j,看是否每个i,f[i][j]都为真,则这个点可以选,统计个数即可。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #include<bitset>
    //by zrt
    //problem:
    using namespace std;
    typedef long long LL;
    const int inf(0x3f3f3f3f);
    const double eps(1e-9);
    int k,n,m;
    int H[1005],tot,P[10005],X[10005];
    bitset<1005> sum[105];
    inline void add(int x,int y){
        P[++tot]=y;X[tot]=H[x];H[x]=tot;
    }
    bool is[1005];
    void dfs(int x,int a){
        sum[a][x]=1;
        for(int i=H[x];i;i=X[i]) if(!sum[a][P[i]]) dfs(P[i],a);
    }
    int num[105];
    int main(){
        #ifdef LOCAL
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
        #endif
        scanf("%d%d%d",&k,&n,&m);
        for(int i=1;i<=k;i++){
            scanf("%d",&num[i]);
        }
        for(int i=1,x,y;i<=m;i++){
            scanf("%d%d",&x,&y);
            add(x,y);
        }
        for(int i=1;i<=k;i++){
            dfs(num[i],i);
        }
        int ans=0;
        for(int i=1;i<=n;i++){
            bool ok=1;
            for(int j=1;j<=k;j++){
                if(!sum[j][i]) {
                    ok=0;break;
                }
            }
            if(ok) ans++;
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    gdb常用命令
    gdb之watch命令
    gdb之x命令
    python's descriptor II
    MacOSX快捷键
    主题敏感词PageRank
    shell调试选项
    shell输出调试信息
    事务时间如何去掉wasted time
    深刻剖析VuGen脚本录制原理
  • 原文地址:https://www.cnblogs.com/zrts/p/bzoj1648.html
Copyright © 2020-2023  润新知