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

链接: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:

/*
 * 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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值