#SRD0000110. Kỳ nghỉ tháng Năm (May Holidays)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Kỳ nghỉ tháng Năm (May Holidays)
May Holidays
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A company has employees, numbered from to . Employee is the head manager. For every employee , employee is their unique direct manager. The management relation forms a rooted tree with root : repeatedly following parent links from any employee eventually reaches employee .
Employee is a subordinate of if following one or more parent links from reaches . Thus an employee is not their own subordinate. Let be the number of subordinates of employee .
Each employee has a tolerance between and . Employee is displeased on a given day if and only if both conditions hold:
- Employee is not on vacation.
- The number of subordinates of currently on vacation is strictly greater than .
There are days, with exactly one event at the beginning of each day, given by a nonzero integer :
- If , employee starts a vacation on that day.
- If , employee returns to work on that day.
Initially nobody is on vacation. All events are valid: an employee already on vacation cannot start another vacation, and a working employee cannot return from vacation.
Task: After each event, count the number of displeased employees. An employee currently on vacation is never counted as displeased, regardless of how many of their subordinates are absent.
Input
- Line 1: two integers .
- Line 2: integers giving direct managers ( is not required).
- Line 3: integers .
- Line 4: integers , processed in their given order.
Output
Print integers in day order, each equal to the number of displeased employees after that day's event. Separate the integers by spaces or newlines.
Subtasks
- Subtask 1 (15 points): ; ; for every , the edges form a tree rooted at ; where is the number of proper subordinates; for every ; vacation/return events are valid and initially nobody is absent.
- Subtask 2 (20 points): ; ; for every , the edges form a tree rooted at ; where is the number of proper subordinates; for every ; vacation/return events are valid and initially nobody is absent.
- Subtask 3 (25 points): ; ; for every , the edges form a tree rooted at ; where is the number of proper subordinates; for every ; vacation/return events are valid and initially nobody is absent.
- Subtask 4 (40 points): ; ; for every , the edges form a tree rooted at ; where is the number of proper subordinates; for every ; vacation/return events are valid and initially nobody is absent.
Examples
Example 1
Input
7 8
4 5 1 1 5 5
0 0 0 1 2 0 0
2 6 3 7 -2 4 -3 1
Output
1 1 1 2 2 2 1 0
Explanation
The tree has root , with , , and . On day 1, employee goes on vacation, so employee (whose tolerance is ) becomes displeased. After the next two days, the count is still . On day 4, employee goes on vacation, leaving employee with three absent subordinates, more than tolerance , so the answer becomes . Day 5 brings employee back but the head manager is still displeased. Day 6 sends employee on vacation; employee cannot be counted while absent. Day 7 returns employee , reducing employee 's absent subordinates to two, so the answer becomes . Day 8 sends employee on vacation, leaving no displeased employees.
Example 2
Input
5 6
1 2 3 4
4 0 0 1 0
1 5 2 3 -5 -1
Output
0 2 1 0 0 0
Explanation
The hierarchy is a chain . Employee has tolerance , so their absent-subordinate count cannot exceed this limit. On day 1, employee goes on vacation; the answer is . On day 2, employee goes on vacation, causing working employees and to exceed their zero tolerance and giving answer . On day 3, employee also takes leave, so they can no longer be counted and the result is . On day 4, employee goes on leave, and no remaining working employee exceeds their tolerance; the answer is . The final two returns do not make anyone displeased.