We need to parse the problem carefully. It's a typical problem about pairing members based on friendship edges in a tree (or forest?) where each member has exactly one inviter: p_i is the member who invited i, for i from 1 to N. Initially only member 0 (owner). So we have a tree rooted at 0 with nodes 0..N. But note that N is days open; members invited each day get numbers i, so after N days we have N+1 members (including owner). However the problem asks for each day i (1 ≤ i ≤ N), after adding member i, we have members 0..i. They all are present each day (they visit the store every day thereafter). They want to pair them into as many pairs as possible (pairing two members) such that each pair consists of "friends": each member is shy and only wants to pair with the friend they invited or the friend who invited them. So essentially an edge exists between two members if one invited the other. That is, the tree edges: each node i (except 0) has an edge to its parent p_i. So the friendships are exactly the edges of the tree.
We need to find the maximum number of disjoint pairs (matching) in the tree after each day (i.e., on tree induced by nodes 0..i). But the matching must be such that we maximize the number of pairs? Actually "find out how many pairs you can make when paired to maximize the number of pairs of friends." So basically the size of maximum matching in that tree. Output that maximum number for each day i.
Constraints N up to 5000. So we need to compute maximum matching size for each prefix of nodes (0..i). But the tree is built incrementally: node i is added with edge to p_i (where p_i < i). So it's a rooted tree. We need to compute matching size after each addition.
We can compute matching in a tree efficiently using DP (tree DP for maximum matching). However we need to recompute after each addition. N up to 5000, O(N^2) is acceptable (5000^2=25e6). So we could recompute DP from scratch each day? That would be O(N^2) total? Actually recomputing DP for the whole tree each day would be O(N * N) = O(N^2) if each DP is O(N). Since N=5000, O(N^2) is 25 million, that's fine. But careful: we need to compute DP for each day i, where tree has i+1 nodes. If we run a DP on the whole tree each day, total complexity sum_{i=1..N} O(i) = O(N^2). That's okay for N=5000.
But can we do better? Not necessary. However we need to be careful: tree DP for maximum matching is standard: For each node, we compute dp0 = max matching in subtree when node is not matched to its parent, and dp1 = max matching in subtree when node is matched to its parent (so it cannot be matched to children). Actually for unrooted tree maximum matching, we can do DP on rooted tree: For each node, we compute two states: unmatched (free) or matched to parent. Then combine children: dp0[node] = sum over children of max(dp0[child], dp1[child]) because if node is free, children can be matched or not, but if a child is matched to its parent (i.e., to node), that would mean node matched to child, but that's not allowed because node is free? Wait careful: In the rooted DP, we root the tree at some node (say 0). For each node, we compute:
- f(node, 0): maximum matching size in subtree of node, where node is NOT matched to its parent (i.e., it is free to be matched to one of its children, but not to parent).
- f(node, 1): maximum matching size in subtree of node, where node IS matched to its parent. In this case, node cannot be matched to any child.
Transitions:
If node is matched to parent (state 1), then for each child, we must have the child not matched to its parent (since node is already matched, child can't match to node). So f(node,1) = sum over children of f(child,0).
If node is not matched to parent (state 0), then we have a choice: either node remains unmatched overall, or it matches to one of its children. But we need to compute the max over possibilities. Typically we compute: initially, f(node,0) = sum over children of max(f(child,0), f(child,1)) because each child can be either state (if child is matched to its parent that would be matching child to node, but that's only possible if node chooses to match with that child; we can't simply sum max because matching node to a child would change that child's state to f(child,1) (since child matched to parent) but then for other children we must use f(child,0) because node cannot match to multiple children. So we need to consider whether to take one child to match with node. The standard DP: compute base = sum over children max(f(child,0), f(child,1)). Then consider for each child, if we match node with that child, we gain: 1 (for the edge) + f(child,0) + sum_{other} max(f(other,0), f(other,1)). But f(child,0) is when child is not matched to its parent; if we match node-child, child must be in state matched to parent, which corresponds to f(child,1)? Actually careful: In the DP definitions, f(child,1) is when child is matched to its parent (i.e., node). So if node matches with child, then child's state must be f(child,1). However note that f(child,1) = sum over grandchildren of f(grandchild,0). So we can compute: For node state 0, we have option: do not match node to any child: then total = sum max(f(child,0), f(child,1)) (each child chooses best). Or match node to one child: then for that child we must use f(child,1) + 1 (the edge contributes 1). For other children, since node is matched (to one child) it is not available to match with others, but the children can be in either state? Actually if node is matched to a child, node is not free to match with other children. So other children cannot be matched to node; thus they must be in state where they are not matched to parent (i.e., state 0). Because if a child were in state 1 (matched to parent), that would mean matching child to node, but node is already matched, so conflict. So for other children we must use f(child,0). So total if match with child = 1 + f(child,1) + sum_{other} f(other,0). So we can compute for each child: compare base0 = sum max(f(c,0), f(c,1)). Then we can compute best matching by considering picking one child to match: for each child, alternative = base0 - max(f(c,0), f(c,1)) + f(c,1) + 1 + (sum over other of (f(other,0) - max(...? Actually base0 includes for each child max(...). To compute the value if we match node with child, we need to replace that child's contribution with (1 + f(child,1)) and for all other children we must use f(other,0) instead of max(f(other,0), f(other,1)). So we can precompute sumMax = sum max(f(c,0), f(c,1)) and also sumF0 = sum f(c,0). Then for each child, value = 1 + f(child,1) + (sumF0 - f(child,0)). Because sum over other children f(other,0). So f(node,0) = max( sumMax, max_{child} (1 + f(child,1) + sumF0 - f(child,0) ) ).
But wait, is sumF0 defined as sum f(c,0)? But we need to subtract f(child,0) from sumF0 to exclude that child, then add f(child,1) + 1. So indeed.
However we must be careful: the DP above works for rooted tree. Then the overall maximum matching for the whole tree (rooted at 0) is f(0,0) because root has no parent, so we consider state where it is free (not matched to any parent). That gives the maximum matching size.
Thus for each day i, we have a tree on nodes 0..i. We can compute DP for the whole tree each day. That's O(i) per day, leading to O(N^2) total.
N=5000, fine.
But careful: The tree is built incrementally but we recompute DP from scratch each day. We need to ensure we can easily rebuild the tree adjacency lists each day? We can just maintain the parent list and children lists as we add nodes. But we recompute DP by traversing from root 0 down through children. Since the tree grows only by adding leaf nodes (node i is added as child of p_i, but note p_i could be any earlier node, so node i becomes a child of that parent, possibly not a leaf if later nodes attach to it, but when it's added it's a leaf (since no children yet). So the tree is built by adding nodes with edge to an existing node. So the structure is a tree known in advance. For each day i, we have nodes 0..i. So we can precompute the children lists for the full N, but for each i we only consider nodes up to i. So we need to run DP only on the induced subtree (the set of nodes 0..i). Since edges only exist between nodes with indices ≤ i? Actually when we add node i, the only edge is between i and p_i (p_i < i). So in the subtree for day i, all nodes 0..i and edges among them: each node j (1≤j≤i) has edge to p_j. So it's exactly the induced subtree of the full tree restricted to nodes 0..i, which is a tree (since it's just the full tree pruned of nodes > i). So we can run DP on that tree.
We could do a simple DFS each day from root, only visiting nodes with index ≤ i. That would be O(i) time per day if we traverse all nodes up to i. So total O(N^2). Since N=5000, this is fine. But we need to be careful about recursion depth (N up to 5000, recursion may be okay but to be safe we can use iterative stack).
Alternative: We can compute DP incrementally: when adding a node i as leaf, we can update DP values along the path to root, possibly in O(depth) per addition, leading to O(N^2) worst-case anyway (if tree is a chain depth N). But that may also be okay. But for simplicity, just recompute each day.
However, there is a catch: the maximum matching in a tree can be computed by DP that is O(N). For each day i, we need to compute DP on tree with i+1 nodes. If we recompute from scratch each day, total operations sum_{i=1}^{N} O(i) = O(N^2). With N=5000, that's about 12.5 million nodes processed (since each node counted many times). Actually careful: If we recompute each day, we will run DFS on all nodes 0..i. Over all days, the total number of node visits is sum_{i=1}^{N} (i+1) ≈ N^2/2 = about 12.5 million. That's very manageable.
But note that each DP calculation involves two passes (post-order) which might require building adjacency each time? We can pre-build the full adjacency list for all nodes (0..N). Then for each day i, we perform a DFS from root 0, but we should only traverse nodes with index ≤ i. So we can do a simple DFS that only goes to children whose index ≤ i (since all children have larger indices? Not necessarily: if we add nodes in order, children always have index greater than parent because p_i < i. So yes, each node's children have indices larger than the node. Because for node j, its children are nodes i where p_i = j and i > j. So in the full tree, each node's children have larger numbers. Therefore, if we restrict to nodes up to i, the children of a node that are ≤ i are exactly those children with indices ≤ i. Since children have indices > parent, but they may be > i, so we skip them. So we can DFS recursively, starting from 0, and for each child c of node v (precomputed children list), if c ≤ i then we recurse. So DP can be done recursively.
Thus algorithm:
- Read N and p_1..p_N.
- Build children list: for i from 1 to N, add i as child of p_i.
- For day = 1 to N:
- Compute DP on tree restricted to nodes 0..day.
- Perform post-order traversal (DFS) from root 0, but only exploring children with index ≤ day.
- For each node v visited, compute dp0[v] and dp1[v] as described.
- The answer for this day is dp0[0] (since root has no parent).
- Print answer.
We need to be careful: The DP definitions as above yield correct maximum matching for the tree. But we need to double-check the recurrence.
Let's formalize for rooted tree with root 0 (no parent). For each node v, define:
- f0(v) = maximum matching size in the subtree of v (including v and its descendants) under the condition that v is NOT matched to its parent (so v is free to match with a child or remain unmatched).
- f1(v) = maximum matching size in the subtree of v under the condition that v IS matched to its parent (so v cannot match with any child).
Transitions:
If v is a leaf (no children in the considered subtree):
f1(v) = 0 (since no children, and v is matched to parent, so no further matches)
f0(v) = 0 (since no children, and v is not matched to parent; but could v be matched? No child, so cannot match, so 0)
If v has children (only those with index ≤ day):
Let C = list of children.
Compute:
sum_f0 = sum_{c in C} f0(c)
sum_max = sum_{c in C} max(f0(c), f1(c))
For f1(v): Since v is matched to parent, it cannot match to children. So each child c must be in state where it is NOT matched to its parent (v). So each child must be f0(c). So f1(v) = sum_f0.
For f0(v): Two possibilities:
- v is unmatched (i.e., not matched to any child): then each child can be in either state (since v is free, but if a child is matched to its parent that would be matching with v, but that would require v to be matched to that child. Since v is unmatched, children cannot be matched to v. So they must be in state f0 as well? Wait careful: The definition of state for a child: f0(c) means c is NOT matched to its parent (v). f1(c) means c IS matched to its parent (v). If v is unmatched, then if a child c is in state f1(c), that would imply c is matched to its parent v, which would match v to c. But then v would be matched to c, contradicting that v is unmatched. So if v is unmatched, children cannot be in state f1(c). So they must be in f0(c). However, is that forced? Actually in the DP, the state of a child is defined with respect to the parent (v). If we set v's state as "not matched to parent", that does not preclude v from being matched to a child? Wait, we need to separate: f0(v) means v is NOT matched to its parent. It does not specify whether v is matched to a child. In the DP, we need to consider both possibilities: v could be matched to one of its children, or remain unmatched. So when computing f0(v), we need to consider two subcases: either v is unmatched entirely, or v is matched to one child.
If v is unmatched entirely, then v is not matched to any child, so children cannot be in state f1(c) (since that would require matching child to v). Therefore each child must be in state f0(c). So contribution = sum_{c} f0(c).
If v is matched to a child u, then v is matched, and that child u must be in state f1(u) (since u matched to parent v). For other children c ≠ u, v is not matched to them, so they must be in state f0(c). Also we add +1 for the edge (v,u). So contribution = 1 + f1(u) + sum_{c ≠ u} f0(c).
Thus f0(v) = max( sum_{c} f0(c), max_{u in C} [1 + f1(u) + sum_{c ≠ u} f0(c)] ).
Simplify: Let sum_f0 = sum f0(c). Then second term for a child u = 1 + f1(u) + (sum_f0 - f0(u)). So f0(v) = max( sum_f0, max_{u} (1 + f1(u) + sum_f0 - f0(u)) ).
Thus we don't need sum_max. That's simpler.
But wait: Is there any scenario where v is unmatched but a child is in state f1(c) because that child is matched to its own child? No, state f1(c) specifically means c is matched to its parent (v). So it's only about the edge (v,c). So indeed, if v is unmatched, children cannot be in f1(c). So correct.
Now compare with earlier recurrence using sum_max. That recurrence was for a different definition: sometimes DP definitions use f0 as maximum matching in subtree when node is "available" to be matched to parent, and f1 when it's not. The standard DP for maximum matching on trees uses the same I described: f0 = max matching when node is free (not matched to parent), f1 = when node is taken (matched to parent). Then the recurrences: f1 = sum f0(child); f0 = max( f1, max_{child} (1 + f1(child) + sum f0(other) )? Actually sometimes they define differently. Let's test on a simple tree: a single edge between root and child.
Root 0, child 1. For day 1 (nodes 0,1):
- For leaf 1 (child): f0(1) = 0, f1(1) = 0 (since no children).
- For root 0 with child 1: sum_f0 = f0(1)=0.
- Option1: root unmatched: sum_f0 =0.
- Option2: match root with child: 1 + f1(1) + (sum_f0 - f0(1)) = 1+0+0=1.
So f0(0) = max(0,1) =1. So answer day1 =1, matches example: first output 1.
Now consider day2 with three nodes: say example 1: N=3, p: [0,0,2]. So tree: 0 has children 1,2? Actually p2=0? Wait input: lines: after N=3, p1=0, p2=0, p3=2. So node1 parent0, node2 parent0, node3 parent2. So tree: 0 children 1 and 2; 2 child 3.
Day1: nodes {0,1} => matching size 1 (as above).
Day2: nodes {0,1,2}. 0 children 1,2 (both leaves). Compute:
Leaf 1: f0=0,f1=0; leaf 2: f0=0,f1=0.
Root 0: sum_f0 = 0+0=0.
Option unmatched: 0.
Option match with child1: 1+ f1(1)+ (sum_f0 - f0(1)) =1+0+0=1.
Option match with child2: similarly 1.
Thus f0(0)=1. So answer day2 =1, matches example.
Day3: nodes {0,1,2,3}. Compute:
Node3 (leaf): f0=0,f1=0.
Node2: children: [3]. So for node2:
sum_f0 = f0(3)=0.
f1(2) = sum_f0 =0.
f0(2) = max( sum_f0=0, max_u: 1+f1(3)+ sum_f0 - f0(3)=1+0+0=1) =1.
Node1 (leaf): f0=0,f1=0.
Root 0: children = [1,2].
Compute:
sum_f0 = f0(1)+f0(2) =0+1=1.
f1(0) = sum_f0 =1.
Option unmatched: sum_f0 =1.
Option match with child1: 1 + f1(1) + (sum_f0 - f0(1)) = 1+0+(1-0)=2.
Option match with child2: 1 + f1(2) + (sum_f0 - f0(2)) = 1+0+(1-1)=1.
So f0(0) = max(1,2,1)=2.
Thus answer day3=2, matches example.
So DP works.
Thus implementation straightforward.
But we must be careful: The DP for root state is f0(0) because root has no parent and we do not have restriction that it must be unmatched. f0 allows matching with child, which is fine.
Now, we need to compute DP for each day efficiently. Since children have larger indices than parent, we can do a recursive post-order traversal (DFS) from root 0, skipping nodes > day. But we will call DP for each day separately; the recursion will revisit many nodes. However, we can compute DP in a bottom-up manner by processing nodes in reverse order (from largest index down to 0) because children have larger indices. For each day i, the set of nodes is 0..i. If we process nodes in decreasing order, we can compute DP for each node once we have computed DP for all its children (which have larger indices). But careful: for a given day i, we only include children ≤ i. So if we iterate v from i down to 0, we can compute dp0[v] and dp1[v] using already computed dp for children (which are > v and ≤ i). Since children indices are larger than v, they will have been computed earlier in decreasing order. So we can compute DP in O(i) time per day without recursion. That's efficient and avoids recursion stack.
Pseudo:
-
Precompute children list for all nodes (0..N). Since we know parents, we can store for each node a vector of its children.
-
For day = 1 to N:
Initialize arrays dp0, dp1 of size day+1 (or size N+1 but we only compute for indices up to day). But we can reuse arrays across days, recomputing values for nodes that are newly added or changed. However easier: For each day, we can compute dp0, dp1 from scratch for nodes 0..day using iteration from day down to 0.
For v from day down to 0:
// compute dp0[v] and dp1[v]
sum_f0 = 0
// we need to consider children c of v with c ≤ day
// For each such child, we have already computed dp0[c] and dp1[c] because c > v and we are iterating descending.
For each child c in children[v]:
if c > day: continue
sum_f0 += dp0[c]
// compute f1: dp1[v] = sum_f0
// compute f0: start with unmatched option = sum_f0
best = sum_f0
// consider matching with each child
For each child c in children[v]:
if c > day: continue
// if we match v with c, value = 1 + dp1[c] + (sum_f0 - dp0[c])
val = 1 + dp1[c] + (sum_f0 - dp0[c])
if val > best: best = val
dp0[v] = best
// after loop, answer = dp0[0]
print dp0[0]
Complexity per day: for each node v, we iterate over its children. Total children across all nodes is N (since each node except root has exactly one parent, so total child relationships = N). For each day i, we process nodes 0..i, and for each node we iterate over its children but only those with index ≤ i. Since children indices are > parent index, many children may be > i and we skip them. However, if we do this nested loops for each day, total operations across all days could be O(N^2) as each edge (v, child) is considered only when the child's index ≤ i, i.e., for days i >= child_index. For each edge (v, child) where child index = j, it will be considered for all days i from j to N (since when day >= child index, child is included). So each edge may be processed up to (N - j + 1) times, which could sum to O(N^2) in worst case (like chain: children are each node i parent i-1; each edge (i-1,i) considered for days i..N => total sum_{i=1}^{N} (N - i +1) = O(N^2)). So overall O(N^2) again. That's fine for N=5000.
But we could potentially compute incremental updates to reduce constant, but not needed.
Let's verify with the DP recurrence for leaf: For v with no children (or children all > day), then sum_f0 =0, best=0, dp0[v]=0, dp1[v]=0. Works.
Now, we need to ensure that the DP indeed yields maximum matching. Double-check on more complex trees.
Alternative approach: Maximum matching in a tree can be found greedily via leaf matching? But DP is solid.
Edge Cases:
- N=1: only node 1 added, tree: 0-1. Day1 answer 1.
- N=5000 chain: p_i = i-1. Then answer for each day: maximum matching size in a path of length i+1 (nodes 0..i). For a path, maximum matching size is floor((i+1)/2)? Actually for a path with even number of nodes? Let's compute: For path of nodes 0-1-2-...-i (i+1 nodes). Maximum matching size is floor((i+1)/2) because we can match edges in a way to cover all nodes except possibly one. But is that the DP output? Let's test small: i=1 (nodes 0,1) => floor(2/2)=1. Good. i=2 (nodes 0,1,2) => floor(3/2)=1. Actually path of 3 nodes max matching is 1 (choose edge (0,1) or (1,2)). DP gave? In chain example earlier? We had chain with p_i = i-1? Let's test day2: nodes 0,1,2. Parents: 1 parent0, 2 parent1? Actually if p_i = i-1, then tree: 0-1-2. Day2: matching? We can match edge (0,1) or (1,2), but not both because node1 would be used twice. So max=1. So answer day2=1. But example input 2? Actually the second example given: Input 5, p: 0,1,2,3,4 (chain). Output: 1,1,2,2,3. Let's check: day1:1, day2:1, day3:2, day4:2, day5:3. For chain length nodes: day3: nodes 0-1-2-3 (4 nodes) max matching = floor(4/2)=2, correct. day4: nodes 0..4 (5 nodes) max matching floor(5/2)=2, correct. day5: nodes 0..5 (6 nodes) floor(6/2)=3, correct. So chain matches.
Thus DP works.
Implementation details:
- We'll store children list as vector<vector> children(N+1) (since nodes 0..N). For i from 1 to N, children[p_i].push_back(i).
- For each day i from 1 to N:
- Compute arrays dp0, dp1 of length i+1 (or N+1 but we only fill up to i). Since indices up to i, we can allocate new vectors each day, but that would be O(N^2) memory allocation overhead. Better to allocate arrays of size N+1 and reuse; we need to ensure that values for nodes > i are not used. We can compute dp0[v] and dp1[v] only for v ≤ i, and for v > i we ignore. But since we iterate v from i down to 0, and for each v we look at children c ≤ i, we might reference dp0[c] which we have computed for c > v (since c ≤ i). So it's fine to have dp arrays of size N+1, and we compute only for indices up to i. However we must ensure that dp values for nodes > i are not used; we won't access them because we skip children > i. So we can keep global dp0, dp1 arrays of size N+1, and for each day recompute values for nodes 0..i (overwriting previous). That works.
But careful: When we compute dp for v, we need dp0[c] and dp1[c] for children c ≤ i. Those are already computed for this day because we process in descending order. So we can compute dp[v] and store in the arrays. However we must make sure that we don't use stale dp values from previous day for nodes that are not recomputed yet. Since we process descending, we will compute all children (which are larger indices) before parent, and we are overwriting dp0[c] and dp1[c] for those nodes with new values for the current day. So that's fine.
But what about nodes that are not included in this day (v > i)? They might have old values from previous day; but we never access them because we never iterate v > i. However, we might accidentally access them via children? For a node v ≤ i, its child c might be > i, but we skip if c > i. So safe.
Therefore we can have global arrays dp0, dp1 of length N+1, initialize to 0. Then for day = 1..N:
For v = day down to 0:
sum_f0 = 0
For each child c in children[v]:
if c > day: continue
sum_f0 += dp0[c] // dp0[c] computed for this day already (since c > v)
dp1[v] = sum_f0
best = sum_f0
For each child c in children[v]:
if c > day: continue
val = 1 + dp1[c] + sum_f0 - dp0[c]
if val > best: best = val
dp0[v] = best
Output dp0[0]
But wait: In computing sum_f0, we used dp0[c]. That's correct because we need f0(c) (child not matched to parent). For leaf children, dp0 is computed earlier.
Potential issue: When we compute dp0[v] we used sum_f0 which is sum of dp0[c] over children. But inside the second loop we also use dp1[c] and dp0[c]. That's fine.
Now complexity: For each day, we loop over all nodes v from day down to 0, and for each v we iterate over its children. The total number of child checks across all days can be high but within O(N^2). Since N=5000, it's okay.
But we might be able to optimize by precomputing something? Not needed.
We should double-check for correctness on some more trees.
Test small N=2 with p = [0,1] (chain 0-1-2). Day1: nodes0,1 => dp0[0]? compute:
- day1: v from 1 down to 0.
v=1: children of 1: [2? but 2>1 so skip]. sum_f0=0 -> dp1[1]=0, dp0[1]=0.
v=0: children of 0: [1] (1 ≤1). sum_f0 = dp0[1]=0. dp1[0]=0. best = sum_f0=0; for child1: val = 1+dp1[1]+0-0 =1 -> best=1. dp0[0]=1. Output 1.
Day2: nodes0,1,2. Process v=2: children: none? Actually 2's children maybe none; sum_f0=0 -> dp1[2]=0,dp0[2]=0.
v=1: children: [2] (2≤2). sum_f0 = dp0[2]=0; dp1[1]=0; best=0; child2: val=1+dp1[2]+0-0=1 => best=1; dp0[1]=1.
v=0: children: [1] (1≤2). sum_f0 = dp0[1]=1; dp1[0]=1; best=1; child1: val=1+dp1[1]+1- dp0[1]? dp1[1]=0, dp0[1]=1 => val=1+0+1-1=1. So best remains 1; dp0[0]=1. Output 1. That matches chain day2 expected? chain day2 output? For chain 0-1-2, maximum matching is 1 (choose edge (0,1) or (1,2)). So yes.
Now day3 if we had chain up to 3: p = [0,1,2] (nodes0-1-2-3). Compute: should yield 2. Works.
Now test a more complex tree.
Consider star: root 0 with many children. For day i with root plus i children? Actually N=5, p1=0, p2=0, p3=0, p4=0, p5=0. That's star with root 0 and children 1..5. For star, maximum matching is 1 because you can match root with one child, and other children remain unmatched (since no edges between leaves). Actually you can only match one edge because root can be in only one pair. So max matching = 1 for any star with at least one leaf? But wait, could we match two leaves? No, leaves are not connected to each other. So max matching = 1. However, our DP for star: For root with many children leaves:
- For each leaf: dp0=0, dp1=0.
- For root: sum_f0 = 0 (since dp0 of all leaves =0). f1(root)=0. f0(root) = max(0, max over children: 1+0+0-0 =1) =1. So dp0[0]=1. So answer always 1 after first child, stays 1. That seems correct.
Now consider a tree where matching can be larger: e.g., a path of length 5 yields matching 2 etc.
Another test: a "double edge" shape: 0-1-2, and also 0-3. So nodes: 1 child of 0, 2 child of 1, 3 child of 0. For day 3 (nodes 0,1,2,3). Compute maximum matching? You can match (0,1) and (2,3?) but 2-3 not edge. Actually edges: (0,1), (1,2), (0,3). So you can match (0,1) and (2,3) not possible because 2-3 not connected. You could match (1,2) and (0,3) simultaneously? Check: (1,2) uses nodes 1,2; (0,3) uses nodes 0,3; no overlap. So that yields 2. Is that valid? Yes. So max matching = 2. DP should compute that. Let's test manually: node2 leaf; node3 leaf; node1: children [2]. So dp0[2]=0,dp1[2]=0; dp0[1] = max(sum_f0=0, 1+dp1[2]+0-0=1) =1; dp1[1]=0. Node3 leaf: dp0[3]=0,dp1[3]=0. Root0 children [1,3]. sum_f0 = dp0[1]+dp0[3]=1+0=1. f1=1. f0 unmatched: sum_f0=1. match with child1: val =1+dp1[1]+ sum_f0 - dp0[1] =1+0+1-1=1. match with child3: val =1+dp1[3]+1-0 =2. So best =2. So dp0[0]=2. Good.
Thus DP works.
Now, consider the possibility that root could be unmatched and some child matches with its own child yielding more matches than matching root with child. The DP accounts for that via the unmatched option and the match option.
Thus solution is correct.
Now implementation:
We'll read N, then p[1..N]. We'll create children vector of size N+1. For i=1..N, children[p_i].push_back(i).
We'll allocate vectors dp0 and dp1 of size N+1 (maybe use arrays). We'll also maybe store sum_f0 and best.
We need to be careful about integer types: N up to 5000, matching size up to floor((N+1)/2) <= 2500, so int fine.
Now we need to output each day's answer.
Complexities: O(N^2) time, O(N) memory.
Edge Cases: p_i can be 0. Owner is 0. Day 0? Not needed.
Check with sample inputs.
We'll implement and test.
But wait: The problem statement: "members invited on the i day will be assigned a membership number i, and the members will visit the store every day thereafter, including the day they are invited." So on day i, the newly invited member i also visits that day? That means all members 0..i are present. So our set of nodes is 0..i inclusive. Good.
Now, are there any additional constraints about pairing? "all members are shy and only want to pair with the friend they invited or the friend they invited." The phrase: "only want to pair with the friend they invited or the friend they invited." Possibly a translation: "only want to pair with the friend they invited or the friend who invited them." That's exactly the edges.
Also: "Since there is only a table for two in this restaurant, members eat in pairs as much as possible. Since the owner also participates in the meal, other members can also pair with the owner. However, all members are shy and only want to pair with the friend they invited or the friend they invited." So they only pair along edges.
Thus maximum matching.
Now, confirm that the DP we derived indeed yields the maximum matching size for the whole tree (unrooted) when we root at 0? Usually maximum matching in a tree can be computed by DP with states: dp[v][0] = max matching in subtree when v is not matched to any child (but could be matched to parent?), we used states relative to parent. That's standard and yields the correct global maximum when root is free.
One nuance: In our DP, for node v in state 0 (not matched to parent), we allow v to be matched to at most one child, which is correct because a node can be incident to at most one matching edge. The DP ensures that if v matches to a child, then that child is forced to state 1 (matched to parent), and all other children must be state 0. That's correct. Also the unmatched option forces all children to be state 0. But is it possible that v is unmatched and some child is in state 1? That would mean that child is matched to its parent v, implying v is matched to that child, contradiction. So indeed children cannot be in state 1 if v is unmatched. So our DP covers all possibilities.
Thus answer is dp0[0] at each day.
Now, is there any subtlety about the root being matched? dp0[0] allows root to be matched to a child (since that case is considered in the max over children). That's correct.
Now, we need to output answers for each day from 1 to N. So for day i, compute and print.
Now we need to confirm that our DP approach, as implemented with descending order, indeed computes correct dp values for nodes in the restricted subtree. Since children have larger indices, processing from day down to 0 ensures that when we compute dp for node v, all its children (c > v) have been computed for the current day (since we processed from higher indices down). However, note that for a given day i, we only compute dp for nodes v from i down to 0. For a child c of v, c > v, so if c ≤ i, then c is in the range and we would have computed dp[c] earlier (since we are going descending). Yes.
But what about node v that has a child c that is > i? We skip those children because they are not present in the current day's tree. That's fine.
Now, what about children of a node that are not direct children? Not needed.
Implementation details:
We'll store children as vector<vector> children(N+1). Since N=5000, adjacency small.
We'll allocate dp0 and dp1 as arrays of int of size N+1 (or maybe vector). We'll also maybe need to reset dp values for nodes > i? Not necessary as they are not used.
But careful: When we compute dp for day i, we compute for v from i down to 0. For each v, we compute using children c ≤ i. That's fine. But dp0[c] and dp1[c] we just computed earlier in this day's loop. However, what about nodes that are not computed in this day's loop but have indices > i? They may contain old dp values from previous day. But they are not accessed because we skip children > i. However, what about a node v that is computed earlier (like v = i) and its children are all > i (so none considered). That's fine.
But there is a subtle issue: Suppose we compute dp for day i, and later for day i+1, we will compute for v from i+1 down to 0. When we compute for v = i+1, we compute dp for that node (which is a new node). For its children (if any) that are ≤ i+1, but children of i+1 would have indices > i+1 (since children have larger indices). So its children are not present yet. So dp for i+1 is leaf (if it has no children within day i+1). That's fine.
But when we compute for v = something less than i+1, we may rely on dp values of children that may have changed from previous day because they were recomputed for this day. That's handled because we recompute all v down to 0 each day, so dp values for all nodes up to i are recomputed based on the current subtree (including new node). So we must ensure that when we compute v, we use dp of children that are already recomputed for this day. Since we process descending, that holds.
Thus algorithm is correct.
Now, we must handle input and output.
Potential pitfalls:
- The root 0 may have many children. Its DP computation loops over all its children, which can be up to N. That's okay.
- The DP recurrence: best = sum_f0 initially (unmatched case). Then iterate children to consider matching with each. However, is it possible that v is matched to a child that yields same as unmatched? That's fine.
- What about the case where v has no children? Then sum_f0=0, best=0, dp1=0, dp0=0. Works.
Now test on given examples.
Let's code a quick mental test:
Example 1:
N=3, p=[0,0,2]
children: 0:[1,2], 2:[3], others empty.
Day1:
i=1: v from 1 to 0.
v=1: children: none (child indices >1? none) -> sum_f0=0 -> dp1[1]=0, dp0[1]=0.
v=0: children: 1 (≤1). sum_f0=dp0[1]=0. dp1[0]=0. best=0. child1: val=1+dp1[1]+0-0=1 -> best=1. dp0[0]=1. output 1.
Day2:
i=2: v=2: children: [3] but 3>2 skip -> sum_f0=0 -> dp1[2]=0, dp0[2]=0.
v=1: leaf -> dp1[1]=0, dp0[1]=0.
v=0: children: 1(≤2),2(≤2). sum_f0 = dp0[1]+dp0[2]=0+0=0. dp1[0]=0. best=0. child1: val=1+0+0-0=1; child2: val=1+0+0-0=1 => best=1. dp0[0]=1. output 1.
Day3:
i=3: v=3: leaf -> dp1[3]=0, dp0[3]=0.
v=2: children: [3] (≤3). sum_f0=dp0[3]=0. dp1[2]=0. best=0; child3: val=1+0+0-0=1 => best=1 -> dp0[2]=1.
v=1: leaf -> dp1[1]=0, dp0[1]=0.
v=0: children:1,2. sum_f0=dp0[1]+dp0[2]=0+1=1. dp1[0]=1. best=1. child1: val=1+dp1[1]+1-dp0[1]=1+0+1-0=2; child2: val=1+dp1[2]+1-dp0[2]=1+0+1-1=1. best=2 -> dp0[0]=2. output 2.
Matches.
Example 2: chain N=5, p: 0,1,2,3,4.
We can test quickly:
Day1: output 1
Day2: output 1
Day3: output 2
Day4: output 2
Day5: output 3
Seems consistent.
Now let's test a more complex scenario to verify recurrence further: consider tree where root has two children, each having a child. So nodes: 0 children 1,2; 1 child 3; 2 child 4. This is like two separate branches. Maximum matching? Could match (1,3) and (2,4) giving 2, or match root with one child and the other branch's child-match etc. Let's compute DP for day 4 (nodes 0..4). Let's compute manually:
- Node3 leaf: dp0=0,dp1=0
- Node4 leaf: dp0=0,dp1=0
- Node1: children [3] -> dp0[1]=1, dp1[1]=0
- Node2: children [4] -> dp0[2]=1, dp1[2]=0
- Node0: children [1,2]; sum_f0 = dp0[1]+dp0[2]=1+1=2; dp1[0]=2; best unmatched =2; match with child1: val=1+dp1[1]+sum_f0-dp0[1]=1+0+2-1=2; match with child2: similarly 2. So dp0[0]=2. So answer 2. But is there a matching of size 2? Yes, (1,3) and (2,4). So fine.
Now consider tree where root has one child that has two children. nodes: 0-1-2, and 0-1-3 (so 1 children 2,3). Actually root 0 child 1, node 1 children 2 and 3. So tree: 0-1-2 and 0-1-3. For day 3 (nodes 0,1,2,3). Max matching? Options: can match (1,2) and (0,?) but 0 cannot match with 1 because 1 used, and 0 cannot match with 3? Actually 0 connected only to 1. So if we match (1,2), then nodes used:1,2. Remaining:0,3. They are not connected (0-1 not available since 1 used, 3 is only connected to 1 which is used). So that yields only 1. If we match (0,1), then we cannot match 2 or 3 because they only connect to 1 (used). So also 1. Could we match (1,3) similarly. So maximum is 1. However, we could match (1,2) and (0,?) no. So max=1. But maybe (0,1) and then 2 and 3 cannot pair =>1. So max=1. Let's test DP:
- Node2 leaf:0,0
- Node3 leaf:0,0
- Node1: children[2,3]. sum_f0 = 0+0=0. dp1[1]=0. best unmatched=0; match with child2: val=1+dp1[2]+0-0=1; match with child3: similarly 1. So dp0[1]=1. dp1[1]=0.
- Node0: child[1]. sum_f0 = dp0[1]=1. dp1[0]=1. best unmatched =1; match with child1: val=1+dp1[1]+1-dp0[1]=1+0+1-1=1. So dp0[0]=1. So answer 1.
Now consider a more interesting tree: root 0, child 1, child 2, and child 2 has child 3. So tree: 0 connected to 1 and 2; 2 connected to 3. We computed earlier gave answer 2 (match 0-3? no 0-3 not edge). Actually we computed earlier with nodes 0,1,2,3 gave dp0[0]=2. That is correct: can match (1,0?) Actually edges: (0,1), (0,2), (2,3). Matching can be (0,1) and (2,3) gives 2. Or (0,2) and (1) unmatched gives 1, or (0,1) and (0,2) not possible. So max=2. So DP gave 2.
Now consider root with three children each with a child, like 0-1-2, 0-3-4, 0-5-6. For nodes up to 6, maximum matching? Possibly match each child with its grandchild: (1,2), (3,4), (5,6) gives 3, root unmatched. Also could match root with one child and the other two grandchildren with their parents? e.g., root match with 1, then 2 can't match, but 3-4,5-6 gives 3? Actually if root matches with 1, then 1 is used, so 2 cannot match with 1, but 2 is leaf and only connected to 1, so 2 stays alone. So we get (0,1), (3,4), (5,6) = 3. So still 3. So max=3. DP should produce 3.
Now test scenario: root with two children, one child has two children. Example: 0-1, 0-2, and 2 has children 3 and 4. Nodes 0..4. Graph edges: (0,1), (0,2), (2,3), (2,4). Maximum matching? Could we match (2,3) and (2,4)? No, 2 can only be in one edge. Options: match (0,1) and (2,3) yields 2; or match (0,2) and (3,4) not connected; or (0,1) and (2,4) yields 2; or (0,2) and (1) and (3,4) not; or match (2,3) and maybe (0,1) =2; or match (2,4) and (0,1)=2; or match (0,2) alone =1; or match (0,1) alone =1; or (2,3) alone =1; or (2,4) alone=1. Could we get 3? Possibly (0,1), (2,3), and what about (4)? no partner. So max=2. DP: compute:
- Node3 leaf:0,0; Node4 leaf:0,0; Node1 leaf:0,0.
- Node2: children [3,4]. sum_f0=0; dp1[2]=0; best unmatched=0; match with 3: val=1+0+0-0=1; match with 4:1 => dp0[2]=1, dp1[2]=0.
- Node0: children [1,2]. sum_f0 = dp0[1]+dp0[2] =0+1=1. dp1[0]=1. best unmatched=1. match with child1: val=1+dp1[1]+1-dp0[1]=1+0+1-0=2; match with child2: val=1+dp1[2]+1-dp0[2]=1+0+1-1=1. So best=2 => dp0[0]=2. So output 2.
All good.
Thus DP works.
Now, we need to ensure that our iterative computation from high to low indeed computes dp0 and dp1 for each node correctly given that children are larger indices.
Potential issue: Node may have multiple children; we compute sum_f0 by iterating children and adding dp0[c] if c ≤ day. But dp0[c] is the value computed in the current day's pass. Since we process descending, c > v, and c ≤ day, so c has been processed earlier in the loop (since we start from day down to 0). So dp0[c] and dp1[c] are up to date for the current day. Good.
Now, we might need to consider that for nodes that are leaves (no children), sum_f0 = 0, dp1=0, dp0=0 (since no child to match). However, note that if a node has children but they are all > day, we treat as leaf for that day.
Now, we need to output for each day i.
Implementation steps:
- Read N.
- children = vector<vector>(N+1);
- For i=1 to N: read p_i; children[p_i].push_back(i);
- Initialize dp0, dp1 as vectors of size N+1, maybe fill with 0.
- For day = 1 to N:
For v = day down to 0:
sum_f0 = 0
for each c in children[v]:
if c > day: continue
sum_f0 += dp0[c]
dp1[v] = sum_f0
best = sum_f0
for each c in children[v]:
if c > day: continue
val = 1 + dp1[c] + (sum_f0 - dp0[c])
if val > best: best = val
dp0[v] = best
Print dp0[0] (and newline)
But note: dp0[0] is computed each day. That's it.
We should be careful: In the inner loops, we compute sum_f0 by iterating over children twice. Could combine but fine.
Edge Cases: What about day=0? Not needed.
Now, test with N=1. p1=0.
children[0] = [1]; others empty.
day=1:
v=1: children[1] empty => sum_f0=0 => dp1[1]=0, dp0[1]=0.
v=0: children[0] -> c=1 (≤1). sum_f0 = dp0[1]=0. dp1[0]=0. best=0. val = 1+dp1[1]+0-0 =1 => best=1. dp0[0]=1. Output 1. Good.
Now, check for N=0? Not possible because N>=1.
Now, consider scenario where root 0 has no children? But p_i are given for i>=1, and p_i can be 0, but if N=0? Not. At least one node will be added. So root will have at least one child eventually.
Now, complexity: For each day i, we iterate v from i down to 0, and for each v we iterate all its children. However, each child list may be large for root. But total number of child iterations across all days is sum_{i=1}^{N} sum_{v=0}^{i} (number of children of v with index ≤ i). This can be upper bounded by sum_{v=0}^{N} (number of children of v) * (N - min_child_index(v) + 1)? Actually each child c will be processed when v is its parent, and for each day i where c ≤ i, we will process that child in the loops for v. So each child contributes to processing for each day i from c to N. So total operations = sum over all child-parent edges (v, c) of (N - c + 1). In worst case, if the tree is a chain, each edge (i-1, i) for i from 1 to N: then contributions = sum_{i=1}^{N} (N - i + 1) = N(N+1)/2 ≈ 12.5M for N=5000. That's fine.
But note that inside the loop for each v we also iterate children twice (for sum_f0 and for best). But we can compute sum_f0 once and then reuse, as we did.
Thus total operations roughly 2 * 12.5M = 25M, which is okay.
Now, let's consider the memory: children list: total N entries. dp arrays: two arrays of size N+1. So fine.
Now we must output each answer line by line.
Now, confirm correctness of DP one more time: Does dp0[v] definition exactly match maximum