#C10224. Delivery Distance Queries
Delivery Distance Queries
Delivery Distance Queries
You are given a city with N intersections and M bidirectional roads connecting them. Initially, no intersection is marked as a delivery point. You are also given Q queries. Each query is one of the following types:
- Type 0: Query the shortest distance from a given intersection to any delivery point. The distance is measured as the minimum number of roads traversed. If no delivery point is reachable, output \(-1\).
- Type 1: Mark the given intersection as a delivery point.
For each query of type 0, compute the shortest distance using a Breadth-First Search (BFS). The roads are unweighted (each road contributes a distance of 1). Note that intersections are 1-indexed.
inputFormat
The input is read from stdin
and has the following format:
- The first line contains two integers N and M -- the number of intersections and the number of roads.
- The next M lines each contain two integers u and v describing a road between intersections u and v.
- The next line contains an integer Q -- the number of queries.
- The following Q lines each contain two integers: the first integer is the query type (either 0 or 1) and the second integer is the intersection number.
All intersections are numbered from 1 to N.
outputFormat
For each query of type 0, output the shortest distance from the specified intersection to any delivery point on a separate line. If no delivery point is reachable, output \(-1\). There is no output for queries of type 1.
## sample6 5
1 2
2 3
3 4
4 5
5 6
7
0 1
1 4
0 2
1 5
0 2
0 6
0 7
-1
2
2
1
-1
</p>