zoukankan      html  css  js  c++  java
  • POJ 3256 Cow Picnic

    Cow Picnic
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 4928   Accepted: 2019

    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: KN, 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

    思路:DFS全图,记录每个牧场可以到达的牛的数量,若pa[v] == K,则所有牛可以到达。


     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #define MAX 10005
     5 using namespace std;
     6 typedef struct{
     7     int to, next;
     8 }Node;
     9 Node edge[MAX];
    10 int head[1005], vis[1005], pa[MAX], in[MAX];
    11 void dfs(int s){
    12     for(int i = head[s];i != -1;i = edge[i].next){
    13         int v = edge[i].to;
    14         if(!vis[v]){
    15             pa[v] ++;
    16             vis[v] = 1;
    17             dfs(v);
    18         }
    19     }
    20 }
    21 int main(){
    22     int K, N, M, u, v;
    23     /* freopen("in.c", "r", stdin); */
    24     while(~scanf("%d%d%d", &K, &N, &M)){
    25         memset(head, -1, sizeof(head));
    26         memset(pa, 0, sizeof(pa));
    27         for(int i = 1;i <= K;i ++){
    28             scanf("%d", &in[i]);
    29             pa[in[i]] ++;
    30         }
    31         for(int i = 1;i <= M;i ++){
    32             scanf("%d%d", &u, &v);
    33             edge[i].to = v;
    34             edge[i].next = head[u];
    35             head[u] = i;
    36         }
    37         for(int i = 1;i <= K;i ++){
    38             memset(vis, 0, sizeof(vis));
    39             vis[in[i]] = 1;
    40             dfs(in[i]);
    41         }
    42         int ans = 0;
    43         for(int i = 1;i <= N;i ++)
    44             if(pa[i] == K) ans ++;
    45         cout << ans << endl;
    46     }
    47     return 0;
    48 }
    
    
  • 相关阅读:
    python中F/f表达式优于format()表达式
    java8新特性-foreach&lambda
    Java实现多线程的四种方式
    Java中的字符串常量池,栈和堆的概念
    java对象只有值传递,为什么?
    面向对象和面向过程程序设计理解及区别
    String对象为什么不可变
    mybatis_plus插件——生成器
    基于grpc的流式方式实现双向通讯(python)
    Django使用DataTables插件总结
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3616208.html
Copyright © 2011-2022 走看看