#DSU0000079. Các cặp đỉnh còn liên thông (Reachable Pairs)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Các cặp đỉnh còn liên thông (Reachable Pairs)
Reachable Pairs
Source: USACO
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected graph with vertices and edges, along with a binary string of length . At time , vertex and its incident edges are removed. If , first connect every pair of its current neighbors by an edge; if , add no edges. Edges created this way persist for later steps.
Immediately before removing vertex , count the unordered pairs of distinct remaining vertices that can reach each other along a path. Output this count for every .
Each operation acts on the graph remaining after previous operations. When , immediately before removing vertex , a direct edge is added between every pair of its currently present neighbors; these edges remain after vertex disappears. When , the incident edges disappear without creating replacements.
Count pairs of distinct surviving vertices that belong to the same connected component. Each unordered pair is counted once, regardless of how many paths connect its endpoints. Answer corresponds to the state immediately before removal of vertex .
Input
The first line contains . The second line is a binary string of length . The following lines describe edges .
Output
Print lines; line contains the number of connected unordered pairs immediately before operation .
Subtasks
- Subtask 1 (20 points): ; ; ; , .
- Subtask 2 (30 points): ; ; ; , .
- Subtask 3 (50 points): ; ; ; , .
Examples
Input
3 2
111
1 2
1 3
Output
3
1
0
Explanation
Before time , all three vertices are connected, yielding inom{3}{2}=3 pairs. Because , vertices are directly joined before vertex is removed. At time , they form the single reachable pair. Before time , only vertex remains and there are no pairs.