Economic Difficulties (Codeforces 1263F)
Tags: DP, flows

An electrical grid in Berland palaces consists of 2 grids: main and reserve. Wires in palaces are made of expensive material, so selling some of them would be a good idea!Each grid (main and reserve) has a head node (its number is $$$1$$$). Every other node gets electricity from the head node. Each node can be reached from the head node by a unique path. Also, both grids have exactly $$$n$$$ nodes, which do not spread electricity further.In other words, every grid is a rooted directed tree on $$$n$$$ leaves with a root in the node, which number is $$$1$$$. Each tree has independent enumeration and nodes from one grid are not connected with nodes of another grid.Also, the palace has $$$n$$$ electrical devices. Each device is connected with one node of the main grid and with one node of the reserve grid. Devices connect only with nodes, from which electricity is not spread further (these nodes are the tree's leaves). Each grid's leaf is connected with exactly one device.  In this example the main grid contains $$$6$$$ nodes (the top tree) and the reserve grid contains $$$4$$$ nodes (the lower tree). There are $$$3$$$ devices with numbers colored in blue. It is guaranteed that the whole grid (two grids and $$$n$$$ devices) can be shown in this way (like in the picture above):  main grid is a top tree, whose wires are directed 'from the top to the down',  reserve grid is a lower tree, whose wires are directed 'from the down to the top',  devices — horizontal row between two grids, which are numbered from $$$1$$$ to $$$n$$$ from the left to the right,  wires between nodes do not intersect. Formally, for each tree exists a depth-first search from the node with number $$$1$$$, that visits leaves in order of connection to devices $$$1, 2, \dots, n$$$ (firstly, the node, that is connected to the device $$$1$$$, then the node, that is connected to the device $$$2$$$, etc.).Businessman wants to sell (remove) maximal amount of wires so that each device will be powered from at least one grid (main or reserve). In other words, for each device should exist at least one path to the head node (in the main grid or the reserve grid), which contains only nodes from one grid.

Input: The first line contains an integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of devices in the palace.The next line contains an integer $$$a$$$ ($$$1 + n \le a \le 1000 + n$$$) — the amount of nodes in the main grid.Next line contains $$$a - 1$$$ integers $$$p_i$$$ ($$$1 \le p_i \le a$$$). Each integer $$$p_i$$$ means that the main grid contains a wire from $$$p_i$$$-th node to $$$(i + 1)$$$-th.The next line contains $$$n$$$ integers $$$x_i$$$ ($$$1 \le x_i \le a$$$) — the number of a node in the main grid that is connected to the $$$i$$$-th device.The next line contains an integer $$$b$$$ ($$$1 + n \le b \le 1000 + n$$$) — the amount of nodes in the reserve grid.Next line contains $$$b - 1$$$ integers $$$q_i$$$ ($$$1 \le q_i \le b$$$). Each integer $$$q_i$$$ means that the reserve grid contains a wire from $$$q_i$$$-th node to $$$(i + 1)$$$-th.The next line contains $$$n$$$ integers $$$y_i$$$ ($$$1 \le y_i \le b$$$) — the number of a node in the reserve grid that is connected to the $$$i$$$-th device.It is guaranteed that each grid is a tree, which has exactly $$$n$$$ leaves and each leaf is connected with one device. Also, it is guaranteed, that for each tree exists a depth-first search from the node $$$1$$$, that visits leaves in order of connection to devices.

Output: Print a single integer — the maximal amount of wires that can be cut so that each device is powered.

Note: For the first example, the picture below shows one of the possible solutions (wires that can be removed are marked in red):  The second and the third examples can be seen below:  

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

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

5
14
1 1 11 2 14 14 13 7 12 2 5 6 1
9 8 3 10 4
16
1 1 9 9 2 5 10 1 14 3 7 11 6 12 2
8 16 13 4 15

Sample Outputs:
5

6

17

Solution (Python):
from collections import defaultdict, Counter
from functools import lru_cache
from itertools import chain
import io, os, sys
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())

def gen():
    _ = int(input())
    parents = [int(i) for i in input().split()]
    devices = [int(i) for i in input().split()]
    d = {x: i for (i, x) in enumerate(devices)}
    (G, segs) = (defaultdict(list), Counter())
    for (i, x) in enumerate(parents):
        G[x] += [i + 2]

    def dfs(node):
        if not G[node]:
            ans = [d[node], d[node], 1]
        else:
            segments = [dfs(child) for child in G[node]]
            ans = [min((i[0] for i in segments)), max((i[1] for i in segments)), sum((i[2] for i in segments)) + (node != 1)]
        segs[ans[0], ans[1]] = max(segs[ans[0], ans[1]], ans[2])
        return ans
    dfs(1)
    return [(x, y, segs[x, y]) for (x, y) in segs]
G = defaultdict(list)
for (x, y, w) in chain(gen(), gen()):
    G[y + 2].append((x + 1, w))

@lru_cache(None)
def dp(node):
    ans = 0
    for (prev, w) in G[node]:
        ans = max(ans, dp(prev) + w)
    return ans
print(dp(n + 1))

Solution (Python):
from collections import defaultdict, Counter
from functools import lru_cache
from itertools import chain
n = int(input())

def gen():
    _ = int(input())
    parents = [int(i) for i in input().split()]
    devices = [int(i) for i in input().split()]
    d = {x: i for (i, x) in enumerate(devices)}
    (G, segs) = (defaultdict(list), Counter())
    for (i, x) in enumerate(parents):
        G[x] += [i + 2]

    def dfs(node):
        if not G[node]:
            ans = [d[node], d[node], 1]
        else:
            segments = [dfs(child) for child in G[node]]
            ans = [min((i[0] for i in segments)), max((i[1] for i in segments)), sum((i[2] for i in segments)) + (node != 1)]
        segs[ans[0], ans[1]] = max(segs[ans[0], ans[1]], ans[2])
        return ans
    dfs(1)
    return [(x, y, segs[x, y]) for (x, y) in segs]
G = defaultdict(list)
for (x, y, w) in chain(gen(), gen()):
    G[y + 2].append((x + 1, w))

@lru_cache(None)
def dp(node):
    if node == 1:
        return 0
    ans = 0
    for (prev, w) in G[node]:
        ans = max(ans, dp(prev) + w)
    return ans
print(dp(n + 1))

Solution (Python):
from collections import defaultdict, Counter
from functools import lru_cache
from itertools import chain
n = int(input())
a1 = int(input())
parents1 = [int(i) for i in input().split()]
devices1 = [int(i) for i in input().split()]
a2 = int(input())
parents2 = [int(i) for i in input().split()]
devices2 = [int(i) for i in input().split()]

def gen(n, parents, devices):
    d = {x: i for (i, x) in enumerate(devices)}
    (G, segs) = (defaultdict(list), Counter())
    for (i, x) in enumerate(parents):
        G[x] += [i + 2]

    def dfs(node):
        if not G[node]:
            ans = [d[node], d[node], 1]
        else:
            segments = [dfs(child) for child in G[node]]
            ans = [min((i[0] for i in segments)), max((i[1] for i in segments)), sum((i[2] for i in segments)) + (node != 1)]
        segs[ans[0], ans[1]] = max(segs[ans[0], ans[1]], ans[2])
        return ans
    dfs(1)
    return [(x, y, segs[x, y]) for (x, y) in segs]
G = defaultdict(list)
for (x, y, w) in chain(gen(n, parents1, devices1), gen(n, parents2, devices2)):
    G[y + 2].append((x + 1, w))

@lru_cache(None)
def dp(node):
    if node == 1:
        return 0
    ans = 0
    for (prev, w) in G[node]:
        ans = max(ans, dp(prev) + w)
    return ans
print(dp(n + 1))