luogu#P12195. [NOISG 2025 Prelim] Itinerary

[NOISG 2025 Prelim] Itinerary

题目描述

The Scientific Committee is planning to visit nn cities. The nn cities are connected by exactly n1n - 1 roads such that it is possible to move between all pairs of cities using the roads. Road ii is built between cities u[i]u[i] and v[i]v[i].

Each city has its own airport. To begin the trip, the committee will fly from Singapore to one of the cities. To make the most out of their trip in the most efficient way possible, the committee wants to visit each city at least once by using each road exactly twice (once in each direction), before flying back home from the last city they find themselves in. A trip satisfying this condition is called a tour.

For example, let the diagram above represent a map of n=8n = 8 cities. One possible tour starting from city 11 is $1 \to 3 \to 5 \to 3 \to 4 \to 2 \to 8 \to 2 \to 4 \to 6 \to 4 \to 7 \to 4 \to 3 \to 1$. Observe that this tour visits all cities a total of 2n12n - 1 (equal to 1515) times and ends at the same city it started from (city 11). It can be shown that these two properties are true for all tours in all possible maps of cities.

The committee also wants to visit mm events which will happen in order from event 11 to event mm. Event jj will be held in city s[j]s[j]. A city can hold zero, one or multiple events, but no two consecutive events are held in the same city, i.e., s[j]=s[j+1]s[j] = s[j + 1].

A tour that allows the committee to visit all events must visit the cities s[1],s[2],,s[m]s[1], s[2], \ldots, s[m] in order, not necessarily consecutively. Such a tour is called an itinerary. Formally, let t[1],t[2],,t[2n1]t[1], t[2], \ldots, t[2n - 1] be the sequence of cities visited in some tour. A tour is an itinerary if and only if ss is a subsequence of tt. That is, ss can be obtained by deleting zero or more elements from tt and maintaining the order of the remaining elements. Using the same example as above, suppose that m=4m = 4 and s=[3,5,2,7]s = [3, 5, 2, 7], then the tour $1 \to \textbf{3} \to \textbf{5} \to 3 \to 4 \to \textbf{2} \to 8 \to 2 \to 4 \to 6 \to 4 \to \textbf{7} \to 4 \to 3 \to 1$ above is an itinerary because the cities 3,5,2,73, 5, 2, 7 are visited in order during the tour (underlined and marked in bold).

The committee is still deciding which city to start from, but they agree that a city is a good choice to start from if there exists an itinerary that starts from it. For all cities, help the committee determine if there exists at least one itinerary starting from that city.

输入格式

Your program must read from standard input.

The first line of input contains two space-separated integers nn and mm, describing the number of cities and the number of events respectively.

The following n1n - 1 lines of input each contain two space-separated integers. The ii-th of these lines contains u[i]u[i] and v[i]v[i], describing the endpoints of the ii-th road.

The last line of input contains mm space-separated integers s[1],s[2],,s[m]s[1], s[2], \ldots, s[m], describing the cities that are holding events.

输出格式

Your program must print to standard output.

The output should contain nn lines. If there exists an itinerary starting from city ii, then the ii-th line should contain a single integer 11. Otherwise, the ii-th line should contain a single integer 00.

8 4
1 3
2 4
3 4
4 6
5 3
2 8
7 4
3 5 2 7
1
0
1
1
0
1
1
0
8 4
1 3
2 4
3 4
4 6
5 3
2 8
7 4
3 2 5 7
0
0
0
0
0
0
0
0
4 7
1 2
1 3
1 4
2 1 2 1 2 1 2
0
0
0
0
5 2
1 2
2 3
3 4
4 5
2 4
1
1
1
1
1

提示

Subtasks

For all test cases, the input will satisfy the following bounds:

  • 2n2000002 \leq n \leq 200\,000
  • 1m2n11 \leq m \leq 2n - 1
  • 1u[i],v[i]n1 \leq u[i], v[i] \leq n for all 1in11 \leq i \leq n - 1
  • 1s[j]n1 \leq s[j] \leq n for all 1jm1 \leq j \leq m
  • s[j]s[j+1]s[j] \neq s[j + 1] for all 1jm11 \leq j \leq m - 1
  • It is possible to move between all pairs of cities using the roads.

Your program will be tested on input instances that satisfy the following restrictions:

Subtask Marks Additional Constraints
00 Sample test cases
11 77 n1000,m=2n1n \leq 1000, m = 2n - 1
22 1010 u[i]=1,v[i]=i+1u[i] = 1, v[i] = i + 1
33 66 n1000,u[i]=i,v[i]=i+1n \leq 1000, u[i] = i, v[i] = i + 1
44 77 u[i]=i,v[i]=i+1u[i] = i, v[i] = i + 1
55 1414 n1000,m10n \leq 1000, m \leq 10
66 55 n1000n \leq 1000
77 1919 m10m \leq 10
88 1111 s[1]=s[m]s[1] = s[m]
99 2121 No additional constraints

Sample Test Case 1 Explanation

This test case is valid for subtasks 5,6,75, 6, 7, and 99.

This test case is described as an example in the problem statement.

There are itineraries starting from cities 1,3,4,61, 3, 4, 6, and 77. An itinerary starting from city 11 is given in the problem statement.

On the other hand, it can be shown that there are no itineraries starting from cities 2,52, 5, and 88.

Sample Test Case 2 Explanation

This test case is valid for subtasks 5,6,75, 6, 7, and 99.

This test case is the same as the example in the problem statement, except s[2]s[2] and s[3]s[3] are swapped. No itineraries exist at all.

Sample Test Case 3 Explanation

This test case is valid for subtasks 1,2,5,6,7,81, 2, 5, 6, 7, 8, and 99.

Sample Test Case 4 Explanation

This test case is valid for subtasks 3,4,5,6,73, 4, 5, 6, 7, and 99.