#K54937. Maximum Scenic Intersections

    ID: 29864 Type: Default 1000ms 256MiB

Maximum Scenic Intersections

Maximum Scenic Intersections

You are given a directed graph representing a city's intersections and one-way streets. Each intersection is marked as scenic (1) or non‐scenic (0). Your task is to determine the maximum number of scenic intersections that can be found together in a strongly connected component (SCC) of the graph. In other words, you need to partition the graph into its SCCs and, for each component, count the unique scenic intersections. The answer is the maximum scenic count among all components.

The problem can be formally stated as follows:

Let \(m\) be the number of intersections and \(n\) be the number of one-way streets. You are given a list \(s\) of length \(m\), where \(s_i = 1\) if intersection \(i\) is scenic and \(s_i = 0\) otherwise. You are also given \(n\) directed edges, each represented as a pair \((u, v)\) meaning there is a one-way street from intersection \(u\) to intersection \(v\). Find the strongly connected component that contains the maximum number of scenic intersections and output that number.

Note: A strongly connected component of a directed graph is a maximal set of vertices such that every pair of vertices in the set is reachable from each other.

inputFormat

The input is given via standard input in the following format:

  • The first line contains two integers \(m\) and \(n\) representing the number of intersections and one-way streets respectively.
  • The second line contains \(m\) integers (each 0 or 1) indicating whether each intersection is scenic.
  • Each of the next \(n\) lines contains two integers \(u\) and \(v\), denoting a one-way street from intersection \(u\) to intersection \(v\).

outputFormat

Output a single integer on standard output: the maximum number of scenic intersections found in any strongly connected component of the graph.

## sample
5 5
1 0 1 0 1
1 2
2 3
3 4
4 5
5 1
3