There are N observatories in AtCoder Hill, called Obs. 1, Obs. 2, ..., Obs. N. The elevation of Obs. i is H_i. There are also M roads, each connecting two different observatories. Road j connects Obs. A_j and Obs. B_j.
Obs. i is said to be good when its elevation is higher than those of all observatories that can be reached from Obs. i using just one road. Note that Obs. i is also good when no observatory can be reached from Obs. i using just one road.
How many good observatories are there?
Constraints
2 \leq N \leq 10^5
1 \leq M \leq 10^5
1 \leq H_i \leq 10^9
1 \leq A_i,B_i \leq N
A_i \neq B_i
Multiple roads may connect the same pair of observatories.
All values in
input are integers.
Input
Input is given from Standard Input in the following format:
N M
H_1 H_2 ... H_N
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Print the number of good observatories.
Examples
Input
4 3
1 2 3 4
1 3
2 3
2 4
Output
2
Input
6 5
8 6 9 1 2 1
1 3
4 2
4 3
4 6
4 6
Output
3
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
n, m = map(int, input().split())
H = list(map(int, input().split()))
H = [0] + H # 1-based indexing
max_neighbor = [0] * (n + 1)
for _ in range(m):
a, b = map(int, input().split())
# Update max_neighbor for a considering b's H
if H[b-1] > max_neighbor[a]:
max_neighbor[a] = H[b-1]
# Update max_neighbor for b considering a's H
if H[a-1] > max_neighbor[b]:
max_neighbor[b] = H[a-1]
count = 0
for i in range(1, n+1):
current_h = H[i-1]
if max_neighbor[i] == 0:
count += 1
elif current_h > max_neighbor[i]:
count += 1
print(count)
Wait, there's a mistake here. In the code above, when accessing H for a and b, the code uses H[b-1] and H[a-1], but the H list was created as [0] + H_original. So H[i] is the elevation for node i (1-based). For example, the original input is H_1 H_2 ... H_N. So when the code reads them into a list, and then prepends 0, H[1] is H_1, H[2] is H_2, etc.
But in the current code, after H = [0] + H_original, the elevation for node a is H[a], not H[a-1]. So the code is incorrect. For example, if a is 1, then H[a] is H_1. But in the current code, H is stored as [0] followed by the original list. For example, if input is "8 6 9 1 2 1", then H becomes [0,8,6,9,1,2,1]. So H[1] is 8, which is correct for node 1. But the code uses H[a-1], which would be H[0] (0) when a is 1. That's a mistake.
Ah, yes! This is a critical error. The code should use H[a] and H[b], not H[a-1] and H[b-1]. Because the H list is already 1-based. So in the code:
for a and b, H[b] is the elevation of node b, H[a] is the elevation of node a.
So the correct code should replace H[b-1] with H[b], and similarly for a.
But in the code above, when processing road a and b:
if H[b-1] > max_neighbor[a], which is incorrect. It should be H[b].
This is a major bug.
So the correct code is:
n, m = map(int, input().split())
H = list(map(int, input().split()))
H = [0] + H # Now H[1] is the first element of input, etc.
max_neighbor = [0] * (n + 1)
for _ in range(m):
a, b = map(int, input().split())
# Update max_neighbor for a with H[b]
if H[b] > max_neighbor[a]:
max_neighbor[a] = H[b]
# Update max_neighbor for b with H[a]
if H[a] > max_neighbor[b]:
max_neighbor[b] = H[a]
count = 0
for i in range(1, n+1):
current_h = H[i]
if max_neighbor[i] == 0:
count += 1
elif current_h > max_neighbor[i]:
count += 1
print(count)
This way, for a road between a and b, we use H[b] (the elevation of node b) to compare with max_neighbor[a], and vice versa.
Testing the first sample:
Sample 1:
H becomes [0,1,2,3,4]
Processing road 1-3:
a=1, H[b] = H[3] =3 → compare with max_neighbor[1] (0). Update to 3.