atcoder#ARC141E. [ARC141E] Sliding Edge on Torus
[ARC141E] Sliding Edge on Torus
Score : points
Problem Statement
There is an undirected graph with vertices. Initially, it has no edges. For each pair of integers such that , the graph has a corresponding vertex called .
You will get queries, which should be processed in order. The -th query, which gives you four integers , is as follows.
- For each , add an edge between two vertices and . Then, print the current number of connected components in the graph.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print lines. The -th line should contain the number of connected components in the graph at the -th query.
3 3
0 0 1 2
2 0 0 0
1 1 2 2
6
4
4
The first query adds an edge between , between , and between , changing the number of connected components from to .
4 3
0 0 2 2
2 3 1 2
1 1 3 3
14
11
11
The graph after queries may not be simple.
6 5
0 0 1 1
1 2 3 4
1 1 5 3
2 0 1 5
5 0 3 3
31
27
21
21
19