#SRD0000122. Sạt lở đường cáp (Collapse)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sạt lở đường cáp (Collapse)
Collapse
Source: JOI
Version: Phuoc Hung OJ Extended
Problem Statement
There are towns, numbered along a valley. Initially, no communication cables exist. Over days, a cable construction plan is carried out. On day (), the operation is :
- If , install an undirected cable between and . That cable is guaranteed to be absent at the start of the day.
- If , remove the cable between and . That cable is guaranteed to be present at the start of the day.
Suppose a landslide occurs between consecutive towns and . Every cable connecting a town in to a town in becomes unusable. Other cables remain usable.
A base station installed in a town can serve every town in its connected component using the remaining cables. Find the minimum number of base stations needed to serve all towns.
Each of the questions gives : after day is completed, suppose a landslide occurs between towns and . Compute the minimum number of base stations. Each question is hypothetical; it does not alter the cable schedule or other questions.
Input
- The first line contains .
- The next lines contain for day ().
- The final lines contain , in question order.
Output
Print exactly lines. Line contains one integer: the minimum required number of base stations for question .
Subtasks
- Subtask 1 (5 points): ; , ; , , ; , ; an edge can only be added if absent and deleted if present.
- Subtask 2 (30 points): ; , ; , , ; , ; an edge can only be added if absent and deleted if present. All have the same value.
- Subtask 3 (30 points): ; , ; , , ; , ; an edge can only be added if absent and deleted if present. All (only additions).
- Subtask 4 (35 points): ; , ; , , ; , ; an edge can only be added if absent and deleted if present.
Examples
Sample 1
Input
5 8 2
0 0 1
0 1 3
0 2 4
0 4 0
1 1 3
0 0 3
0 1 2
0 4 3
3 1
7 3
Output
3
2
Explanation
After day (), the existing cables are . A landslide at leaves components , and , so stations are needed. After day (), cutting at leaves towns connected and town isolated; the answer is .