#C10799. Kingdom Connectivity
Kingdom Connectivity
Kingdom Connectivity
You are given a kingdom with n cities and n-1 bidirectional roads connecting them. The capital city is city 1. Initially, the cities are connected by the given roads forming a tree.
You need to process Q queries. There are three types of queries:
0 x
: Check if city x is reachable from the capital. If it is, outputYES d
where d is the number of roads (edges) used in the shortest path; otherwise, outputNO
.1 a b
: Close the road between cities a and b. After a road is closed, it cannot be used for travel.2 a b
: Reopen the road between cities a and b. The road becomes available for travel again.
It is guaranteed that initially the roads form a tree and every query of type 0, 1, or 2 is valid. Note that the distance is defined as the number of edges in the shortest path, i.e. \(d\) satisfies \(d=\text{number of roads}\).
Input/Output must be handled through standard input (stdin) and standard output (stdout).
inputFormat
The first line contains an integer n representing the number of cities.
The next n-1 lines each contain two space‐separated integers a and b representing a road between cities a and b.
The following line contains an integer Q denoting the number of queries.
The next Q lines describe the queries. Each query is in one of the following formats:
0 x
1 a b
2 a b
All numbers are separated by spaces or newlines.
outputFormat
For each query of type 0
, output a single line containing the answer. If the city is reachable from the capital, output YES d
where d is the minimum number of roads; otherwise, output NO
.
5
1 2
1 3
3 4
3 5
3
0 4
1 1 3
0 5
YES 2
NO
</p>