Kirill and Company (Codeforces 1741G)
Tags: DP, flows

Kirill lives on a connected undirected graph of $$$n$$$ vertices and $$$m$$$ edges at vertex $$$1$$$. One fine evening he gathered $$$f$$$ friends, the $$$i$$$-th friend lives at the vertex $$$h_i$$$. So all friends are now in the vertex $$$1$$$, the $$$i$$$-th friend must get to his home to the vertex $$$h_i$$$.The evening is about to end and it is time to leave. It turned out that $$$k$$$ ($$$k \le 6$$$) of his friends have no cars, and they would have to walk if no one gives them a ride. One friend with a car can give a ride to any number of friends without cars, but only if he can give them a ride by driving along one of the shortest paths to his house.For example, in the graph below, a friend from vertex $$$h_i=5$$$ can give a ride to friends from the following sets of vertices: $$$[2, 3]$$$, $$$[2, 4]$$$, $$$[2]$$$, $$$[3]$$$, $$$[4]$$$, but can't give a ride to friend from vertex $$$6$$$ or a set $$$[3, 4]$$$.  The vertices where friends without cars live are highlighted in green, and with cars — in red. Kirill wants as few friends as possible to have to walk. Help him find the minimum possible number.

Input: The first line of input data contains an integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases in the test.The first line of the test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 10^4$$$, $$$n-1 \le m \le min (10^4, $$$$$$ \frac{n \cdot (n - 1)}{2} $$$$$$)$$$) — the number of vertices and edges, respectively.The next $$$m$$$ lines of the test case contain a description of the edges, two integers each $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$, $$$u \ne v$$$) — indexes of vertices connected by an edge. It is guaranteed that there is at most one edge between any pair of vertices (i.e. no multiple edges in the graph).Then follows line containing the number $$$f$$$ ($$$1 \le f \le 10^4$$$) — the number of Kirill's friends.The next line of the test case contains $$$f$$$ integers: $$$h_1, h_2, \dots, h_f$$$ ($$$2 \le h_i \le n$$$) — the vertices in which they live. Some vertices may be repeated.The next line of the set contains the number $$$k$$$ ($$$1 \le k \le min(6, f)$$$) — the number of friends without cars.The last line of each test case contains $$$k$$$ integers: $$$p_1, p_2, \dots, p_k$$$ ($$$1 \le p_i \le f$$$, $$$p_i < p_{i+1}$$$) — indexes of friends without cars.It is guaranteed that the sum of $$$n$$$ over all cases does not exceed $$$10^4$$$, as well as the sums of $$$m$$$ and $$$f$$$.

Output: Output $$$t$$$ lines, each of which contains the answer to the corresponding test case. As an answer, output a single integer — the minimum possible number of friends who will have to walk.

Note: The first test case of the first example is explained in the statement.In the second test case of the first example, two friends with cars live at vertex $$$5$$$, one can give a ride to friends from vertices $$$2$$$ and $$$3$$$, and the second from vertex $$$4$$$, only a friend from vertex $$$6$$$ will have to walk.

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

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

Sample Outputs:
2
1
1

3
1
0

Solution (Python):
from collections import deque

def solve():
    (n, m) = map(int, input().split())
    sl = [[] for _ in range(n)]
    for _ in range(m):
        (u, v) = map(int, input().split())
        u -= 1
        v -= 1
        sl[u] += [v]
        sl[v] += [u]
    f = int(input())
    h = [int(x) - 1 for x in input().split()]
    mask = [0] * n
    k = int(input())
    p = [int(x) - 1 for x in input().split()] + [-1]
    for i in range(k):
        mask[h[p[i]]] += 1 << i
    vars = [set() for _ in range(n)]
    dist = [-1] * n
    dist[0] = 0
    vars[0].add(mask[0])
    q = deque([0])
    while len(q) > 0:
        v = q.popleft()
        for u in sl[v]:
            if dist[u] == -1:
                dist[u] = dist[v] + 1
                q.append(u)
            if dist[u] == dist[v] + 1:
                for msk in vars[v]:
                    vars[u].add(msk | mask[u])
    backpack = [False] * (1 << k)
    backpack[0] = True
    j = 0
    for i in range(f):
        if i == p[j]:
            j += 1
            continue
        nw = backpack.copy()
        for msk in range(1 << k):
            if not backpack[msk]:
                continue
            for var in vars[h[i]]:
                nw[msk | var] = True
        (backpack, nw) = (nw, backpack)
    mn = k
    for msk in range(1 << k):
        if not backpack[msk]:
            continue
        ans = 0
        for b in range(k):
            if msk & 1 << b == 0:
                ans += 1
        mn = min(mn, ans)
    print(mn)
t = int(input())
for _ in range(t):
    solve()

Solution (Python):
from collections import deque

def solve():
    (n, m) = map(int, input().split())
    sl = [[] for _ in range(n)]
    for _ in range(m):
        (u, v) = map(int, input().split())
        u -= 1
        v -= 1
        sl[u] += [v]
        sl[v] += [u]
    f = int(input())
    h = [int(x) - 1 for x in input().split()]
    mask = [0] * n
    k = int(input())
    p = [int(x) - 1 for x in input().split()] + [-1]
    for i in range(k):
        mask[h[p[i]]] += 1 << i
    vars = [set() for _ in range(n)]
    dist = [-1] * n
    dist[0] = 0
    vars[0].add(mask[0])
    q = deque([0])
    while len(q) > 0:
        v = q.popleft()
        for u in sl[v]:
            if dist[u] == -1:
                dist[u] = dist[v] + 1
                q.append(u)
            if dist[u] == dist[v] + 1:
                for msk in vars[v]:
                    vars[u].add(msk | mask[u])
    backpack = [False] * (1 << k)
    backpack[0] = True
    j = 0
    for i in range(f):
        if i == p[j]:
            j += 1
            continue
        nw = backpack.copy()
        for msk in range(1 << k):
            if not backpack[msk]:
                continue
            for var in vars[h[i]]:
                nw[msk | var] = True
        (backpack, nw) = (nw, backpack)
    mn = k
    for msk in range(1 << k):
        if not backpack[msk]:
            continue
        ans = 0
        for b in range(k):
            if msk & 1 << b == 0:
                ans += 1
        mn = min(mn, ans)
    print(mn)
t = int(input())
for _ in range(t):
    solve()

Solution (Python):
from collections import deque

def solve():
    (n, m) = map(int, input().split())
    sl = [[] for _ in range(n)]
    for _ in range(m):
        (u, v) = map(int, input().split())
        u -= 1
        v -= 1
        sl[u] += [v]
        sl[v] += [u]
    f = int(input())
    h = [int(x) - 1 for x in input().split()]
    mask = [0] * n
    k = int(input())
    p = [int(x) - 1 for x in input().split()] + [-1]
    for i in range(k):
        mask[h[p[i]]] += 1 << i
    vars = [set() for _ in range(n)]
    dist = [-1] * n
    dist[0] = 0
    vars[0].add(mask[0])
    q = deque([0])
    while len(q) > 0:
        v = q.popleft()
        for u in sl[v]:
            if dist[u] == -1:
                dist[u] = dist[v] + 1
                q.append(u)
            if dist[u] == dist[v] + 1:
                for msk in vars[v]:
                    vars[u].add(msk | mask[u])
    backpack = [False] * (1 << k)
    backpack[0] = True
    j = 0
    for i in range(f):
        if i == p[j]:
            j += 1
            continue
        nw = backpack.copy()
        for msk in range(1 << k):
            if not backpack[msk]:
                continue
            for var in vars[h[i]]:
                nw[msk | var] = True
        (backpack, nw) = (nw, backpack)
    mn = k
    for msk in range(1 << k):
        if not backpack[msk]:
            continue
        ans = 0
        for b in range(k):
            if msk & 1 << b == 0:
                ans += 1
        mn = min(mn, ans)
    print(mn)
t = int(input())
for _ in range(t):
    solve()