#P10231. [COCI 2023/2024 #4] Putovanje

[COCI 2023/2024 #4] Putovanje

Background

Translated from COCI 2023/2024 Contest #4 Task 4 “Putovanje”.

Problem Description

Mr. Malnar has finally reached his annual vacation. The country he decided to visit can be represented as nn cities and mm bidirectional roads connecting them. All roads have the same length, and it is possible to travel from any city to any other city using these roads. A path from city aa to city bb is defined as a sequence of roads such that, starting from city aa, you traverse the roads in that order and eventually arrive at city bb. The length of a path is defined as the number of roads on that path.

As usual, Mr. Malnar booked the most expensive hotel in one of the cities and then started planning his trip. To make planning easier, he wrote down the shortest path length from the hotel to every city.

Because he was excited about his long-awaited vacation, Mr. Malnar completely forgot which city the hotel is in. He certainly does not want to miss this trip, so he asks you to determine which cities the hotel could be located in.

Input Format

The first line contains two integers nn and m (1n5104,n1m105)m\ (1\le n\le 5\cdot 10^4,n-1\le m\le 10^5), representing the number of cities and roads.

The next mm lines each contain two integers ui,vi (1ui,vin,uivi)u_i,v_i\ (1\le u_i,v_i\le n,u_i\neq v_i), meaning there is a road between cities uiu_i and viv_i. There is at most one road between any pair of cities.

The last line contains nn integers. The ii-th integer di (1di<n)d_i\ (-1\le d_i<n) denotes the path length from city ii to the city where the hotel is located; if Mr. Malnar did not record this value, it is 1-1.

Output Format

On the first line, output how many cities the hotel could be located in.

On the second line, output the indices of the cities where the hotel could be located, in increasing order.

7 6
1 2
1 3
3 4
3 5
3 6
5 7
2 -1 -1 -1 -1 -1 3

2
4 6

6 6
1 2
2 3
3 4
4 5
5 6
6 1
2 -1 -1 1 -1 -1

2
3 5

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

0

Hint

Sample Explanation 1

The path length from city 44 to city 11 is 22, and to city 77 is 33. Therefore, city 44 satisfies both conditions, so the hotel can be located there.

The same also applies to city 66.

Subtasks

Subtask ID Additional Constraints Score
11 m+1=n5 000m+1=n\le 5\ 000, and for all ii, ui+1=viu_i+1=v_i 1010
22 For all i>1i>1, di=1d_i=-1 2020
33 n,m5 000n,m\le 5\ 000 3535
44 No additional constraints 4545

Translated by ChatGPT 5