牛客假日团队赛3 -Cow picnic(DFS)

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

链接:https://ac.nowcoder.com/acm/contest/945/G
来源:牛客网

题目描述

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.

输入描述:

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.

输入

2 4 4
2
3
1 2
1 4
2 3
3 4

输出

2

说明

4<–3
^ ^
| |
| |
1–>2
The pastures are laid out as shown above, with cows in pastures 2 and 3.
The cows can meet in pastures 3 or 4.

**题目大意:**很多牛在图中的不同位置,求图中,所有牛都能到的点有多少个。

解题思路:

直接对每个牛dfs,并对节点进行计数,每一个牛能到这个节点就加一,最后看等于k的节点有多少个:

AC Code:

扫描二维码关注公众号,回复: 6654836 查看本文章
/*
 * Copyright (c) 2019 Ng Kimbing, HNU, All rights reserved. May not be used, modified, or copied without permission.
 * @Author: Ng Kimbing, HNU.
 * @LastModified:2019-06-25 T 15:35:29.755 +08:00
 */
package ACMProblems.QianDaoTi;

import java.util.ArrayList;

import static ACMProblems.ACMIO.*;

public class cowDfs {
    private static boolean[] visited;
    private static ArrayList<Integer>[] lists;

    private static void dfs(int i) {
        visited[i] = true;
        for (int next : lists[i]) {
            if (!visited[next])
                dfs(next);
        }
    }

    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws Exception {
        setStream(System.in);
        int k = nextInt();
        int n = nextInt();
        int m = nextInt();
        int[] cows = new int[k];
        lists = new ArrayList[n];
        int[] sum = new int[n];
        visited = new boolean[n];
        for (int i = 0; i < n; ++i)
            lists[i] = new ArrayList<>();
        for (int i = 0; i < k; ++i)
            cows[i] = nextInt() - 1;
        while (m-- != 0) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            lists[x].add(y);
        }
        for (int cow : cows) {
            dfs(cow);
            for (int j = 0; j < n; j++) {
                if (visited[j]) {
                    ++sum[j];
                    visited[j] = false;
                }
            }
        }
        int count = 0;
        for (int i = 0; i < n; i++)
            if (sum[i] == k)
                ++count;
        System.out.println(count);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44090305/article/details/93622037