Police Stations (Codeforces 796D)
Tags: DP, shortest paths

Inzane finally found Zane with a lot of money to spare, so they together decided to establish a country of their own.Ruling a country is not an easy job. Thieves and terrorists are always ready to ruin the country's peace. To fight back, Zane and Inzane have enacted a very effective law: from each city it must be possible to reach a police station by traveling at most d kilometers along the roads.  There are n cities in the country, numbered from 1 to n, connected only by exactly n - 1 roads. All roads are 1 kilometer long. It is initially possible to travel from a city to any other city using these roads. The country also has k police stations located in some cities. In particular, the city's structure satisfies the requirement enforced by the previously mentioned law. Also note that there can be multiple police stations in one city.However, Zane feels like having as many as n - 1 roads is unnecessary. The country is having financial issues, so it wants to minimize the road maintenance cost by shutting down as many roads as possible.Help Zane find the maximum number of roads that can be shut down without breaking the law. Also, help him determine such roads.

Input: The first line contains three integers n, k, and d (2 ≤ n ≤ 3·105, 1 ≤ k ≤ 3·105, 0 ≤ d ≤ n - 1) — the number of cities, the number of police stations, and the distance limitation in kilometers, respectively.The second line contains k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — each denoting the city each police station is located in.The i-th of the following n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the cities directly connected by the road with index i.It is guaranteed that it is possible to travel from one city to any other city using only the roads. Also, it is possible from any city to reach a police station within d kilometers.

Output: In the first line, print one integer s that denotes the maximum number of roads that can be shut down.In the second line, print s distinct integers, the indices of such roads, in any order.If there are multiple answers, print any of them.

Note: In the first sample, if you shut down road 5, all cities can still reach a police station within k = 4 kilometers.In the second sample, although this is the only largest valid set of roads that can be shut down, you can print either 4 5 or 5 4 in the second line.

Sample Inputs:
6 2 4
1 6
1 2
2 3
3 4
4 5
5 6

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

Sample Outputs:
1
5

2
4 5

Solution (Python):
from collections import deque
from sys import stdin
inf = int(1e+18)
input = stdin.readline
if __name__ == '__main__':
    (n, k, d) = map(int, input().split())
    polices = [int(i) for i in input().split()]
    adj = [[] for i in range(n + 1)]
    for i in range(1, n):
        (u, v) = map(int, input().split())
        adj[u].append((v, i))
        adj[v].append((u, i))
    used = [False] * (n + 1)
    path = set()
    frontier = deque()
    for p in polices:
        frontier.append(p)
        used[p] = True
    while len(frontier) > 0:
        node = frontier.popleft()
        for (child, index) in adj[node]:
            if not used[child]:
                used[child] = True
                frontier.append(child)
                path.add(index)
    print(n - 1 - len(path))
    ans = []
    for i in range(1, n):
        if i not in path:
            ans.append(i)
    print(*ans)

Solution (Python):
from collections import deque
import sys
input = sys.stdin.readline
(n, k, d) = map(int, input().split())
ps = list(map(int, input().split()))
adjList = [[] for i in range(n + 1)]
q = deque()
visited = [0] * (n + 1)
roads = set()
for i in range(1, n):
    (u, v) = map(int, input().split())
    adjList[u].append((v, i))
    adjList[v].append((u, i))
for ele in ps:
    q.append(ele)
    visited[ele] = 1
while q:
    node = q.popleft()
    for (neighbour, i) in adjList[node]:
        if not visited[neighbour]:
            visited[neighbour] = 1
            q.append(neighbour)
            roads.add(i)
print(n - 1 - len(roads))
for i in range(1, n):
    if i not in roads:
        print(i, end=' ')

Solution (Python):
import math
from collections import defaultdict, deque, Counter
from sys import stdin
import heapq
inf = int(1e+18)
input = stdin.readline
(n, k, d) = map(int, input().split())
ps = [int(i) for i in input().split()]
adj = [[] for i in range(n + 1)]
for i in range(n - 1):
    (u, v) = map(int, input().split())
    adj[u].append((v, i + 1))
    adj[v].append((u, i + 1))
visit = [False] * (n + 1)
edges = set()
q = deque()
for p in ps:
    q.append(p)
    visit[p] = True
while len(q) > 0:
    node = q.popleft()
    for (child, i) in adj[node]:
        if not visit[child]:
            visit[child] = True
            q.append(child)
            edges.add(i)
print(n - 1 - len(edges))
ans = []
for i in range(n - 1):
    if i + 1 not in edges:
        ans.append(i + 1)
print(*ans)