Holiday teams cattle off season 3 -Cow picnic (DFS)

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Links: https://ac.nowcoder.com/acm/contest/945/G
Source: Cattle-off network

Title 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.

Enter a description:

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.

Output Description:

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

Entry

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

Export

2

Explanation

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.

** subject to the effect: ** different positions a lot of cattle in the figure, seeking figure, all the cattle can point to how many.

Problem-solving ideas:

Node directly to each cow dfs, and the node count, each cow will be able to add a node to this, the final look of k is equal to the number of:

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);
    }
}

Guess you like

Origin blog.csdn.net/weixin_44090305/article/details/93622037
Recommended