#P17066. [ICPC 2017 Shenyang R] Bridge
[ICPC 2017 Shenyang R] Bridge
Problem Description
Consider a grid graph with nodes where and . The initial graph has edges connecting all pairs of adjacent nodes.
You need to maintain the graph with two types of different adjustments. The first one, denoted by “”, adds a new edge between the nodes and which did not exist. The second one, denoted by “”, erases an existing edge between the nodes and .
It is sure that, for each adjustment, and were adjacent in the original grid graph. That is, they either share the same coordinate and , or they share the same coordinate and . After each adjustment, we guarantee the connectedness of the graph, and you need to calculate the number of bridges in the current graph.
Input Format
The first line of input contains an integer () which is the total number of test cases. For each test case, the first line contains integers () and (); indicates the size of the graph and is the number of adjustments. Each of the following lines contains an adjustment described as above.
Only one case satisfies .
Output Format
For each test case, output lines, each of which contains the number of bridges.
2
4 8
2 0 3 1 3
2 0 2 1 2
2 0 4 1 4
1 0 2 1 2
1 0 3 1 3
2 0 1 1 1
1 0 4 1 4
2 1 2 1 3
6 2
2 1 2 1 3
2 0 4 0 5
0
0
7
4
2
4
2
4
1
2