Reachable Pairs
Time limit2sMemory limit2048 MB
Given an undirected graph and a binary string, count the reachable node pairs before each step, where a 1 removes the node and joins its neighbors and a 0 just removes it.
- Level
Hard8 of 10
- Topics
- Graph, Union-find
- Solved
- No attempts yet
Problem
An undirected graph has nodes labeled to and edges (, ). You are given a binary string . For each timestep from to , do the following.
- If , remove node from the graph.
- If , remove node from the graph, and add an edge between every pair of neighbors that node had just before its removal.
In both cases, removing a node also removes all edges incident to it.
For each timestep , count the pairs of nodes that can reach each other through a sequence of edges in the graph just before timestep .
Input
The first line contains and .
The second line contains the binary string of length .
Each of the next lines contains two integers, the endpoints of an edge.
Output
Print lines. The -th line contains the number of pairs of reachable nodes just before timestep .