Domain: Algorithms & Data Structures

You are given an undirected connected graph G with n vertices and exactly n edges. The graph has no self-loops and no multiple edges. All edges have unit weight. Each edge has a state: **on** or **off**. Initially all edges are **off**.

You are given m queries (v, u). For each query, you must **toggle** (change on to off, or off to on) the state of **all edges on the shortest path** from v to u in G (using minimum number of edges).

If there are multiple shortest paths, you must choose the **lexicographically minimal** one, where a path is represented as the vertex sequence v, v1, v2, ..., u and sequences are compared element-by-element from left to right.

After each query, output the number of connected components in the graph that contains all n vertices and **only the edges that are currently on**.

### Input
- First line: n m (3 <= n <= 10, 1 <= m <= 10)
- Next n lines: each line contains two integers a b (1 <= a, b <= n, a != b) describing an edge
- Next m lines: each line contains two integers v u (1 <= v, u <= n, v != u) describing a query

### Output
Print m lines. The i-th line is the number of connected components after the i-th query.

Grading rubric:
criterion: For the input "6 3\n1 2\n2 3\n3 4\n4 1\n2 5\n4 6\n2 4\n5 6\n3 1\n", does the answer output exactly "4\n4\n2"?
weight: 15
criterion: For the input "5 4\n1 2\n2 3\n3 1\n3 4\n4 5\n1 2\n2 3\n1 3\n5 1\n", does the answer output exactly "4\n3\n3\n1"?
weight: 20
criterion: For the input "4 2\n1 2\n2 3\n3 4\n4 1\n1 3\n2 4\n", does the answer output exactly "2\n2"?
weight: 15
criterion: For the input "5 2\n1 2\n2 3\n3 1\n1 4\n4 5\n5 2\n5 3\n", does the answer output exactly "2\n3"?
weight: 15
criterion: For the input "3 3\n1 2\n2 3\n3 1\n1 2\n2 3\n1 3\n", does the answer output exactly "2\n1\n1"?
weight: 10
criterion: Does the answer demonstrate that when two cycle arcs have equal length, the lexicographically smaller path is chosen by comparing the next vertex after the start?
weight: 5
criterion: Does the answer demonstrate state that a connected graph with n vertices and exactly n edges contains exactly one simple cycle?
weight: 5

Answer:
## 1) Graph structure
A connected graph with n vertices and n edges contains exactly one simple cycle. All other edges are bridges forming trees attached to cycle vertices.

Let:
- C = set of cycle vertices, k = |C|
- onTreeEdges = number of ON edges not on the cycle
- onCycleEdges = number of ON edges on the cycle

## 2) Connected components formula
Tree edges alone give a forest:
  compsTrees = n - onTreeEdges

For the cycle on k vertices:
- If onCycleEdges = k (all cycle edges ON): compsCycle = 1
- If onCycleEdges < k: compsCycle = k - onCycleEdges

The k cycle vertices are k distinct components in the tree forest, merged by cycle edges:
  answer = compsTrees + compsCycle - k
  = (n - onTreeEdges) + compsCycle - k

## 3) Path structure
Let c(x) = the cycle vertex reached from x by following tree ancestors (or x itself if on cycle). Any shortest path v -> u consists of:
1. Unique tree path v -> c(v)
2. An arc on the cycle from c(v) to c(u)
3. Unique tree path c(u) -> u

Only the cycle arc direction is ambiguous. Choose the shorter arc. If both arcs have equal length, choose the direction whose next vertex after c(v) on the cycle has the smaller vertex id, which makes the full vertex sequence lexicographically minimal.

## 4) Implementation
Use HLD + segment trees with range flip/sum on tree edges, and a separate segment tree on the k cycle edges. Each query: flip tree paths via HLD, flip cycle arc, then compute answer using the formula above.

Complexity: O((n + m) log n) time, O(n) memory.

## 5) Verification

**Test A**
Input:
6 3
1 2
2 3
3 4
4 1
2 5
4 6
2 4
5 6
3 1

Cycle: 1-2-3-4-1 (k=4). Tree edges: 2-5, 4-6.

Query (2,4): cycle arcs both length 2. Next vertices: 3 vs 1. Choose 1 (path 2-1-4). Toggle 2-1, 1-4. onTree=0, onCycle=2. answer = 6-0+4-2-4 = 4.
Query (5,6): tree 5-2, tree 6-4, cycle 2-1-4 (same tie-break). Toggle tree 5-2, 4-6 ON; cycle 2-1, 1-4 OFF. onTree=2, onCycle=0. answer = 6-2+4-0-4 = 4.
Query (3,1): cycle arcs length 2. Next vertices: 2 vs 4. Choose 2 (path 3-2-1). Toggle 3-2, 2-1. onTree=2, onCycle=2. answer = 6-2+4-2-4 = 2.

Output:
4
4
2

**Test B**
Input:
5 4
1 2
2 3
3 1
3 4
4 5
1 2
2 3
1 3
5 1

Cycle: 1-2-3-1 (k=3). Tree edges: 3-4, 4-5.

Query (1,2): path 1-2 (length 1). Toggle 1-2. onTree=0, onCycle=1. answer = 5+3-1-3 = 4.
Query (2,3): path 2-3 (length 1). Toggle 2-3. onTree=0, onCycle=2. answer = 5+3-2-3 = 3.
Query (1,3): path 1-3 (length 1). Toggle 1-3. onTree=0, onCycle=3. answer = 5+1-3 = 3.
Query (5,1): tree 5-4-3, cycle 3-1 (length 1). Toggle tree 4-5, 3-4; cycle 3-1 OFF. onTree=2, onCycle=2. answer = 5-2+3-2-3 = 1.

Output:
4
3
3
1

**Test C**
Input:
4 2
1 2
2 3
3 4
4 1
1 3
2 4

Pure 4-cycle (k=4).

Query (1,3): arcs both length 2. Next vertex: 2 vs 4. Choose 2 (path 1-2-3). Toggle 1-2, 2-3. onCycle=2. answer = 4+4-2-4 = 2.
Query (2,4): arcs both length 2. Next vertex: 3 vs 1. Choose 1 (path 2-1-4). Toggle 2-1 (ON->OFF), 1-4 (OFF->ON). ON: {2-3, 1-4}. onCycle=2. answer = 4+4-2-4 = 2.

Output:
2
2

**Test D**
Input:
5 2
1 2
2 3
3 1
1 4
4 5
5 2
5 3

Cycle: 1-2-3 (k=3). Tree: 1-4, 4-5.

Query (5,2): tree 5-4-1, cycle 1-2 (length 1). Toggle tree 4-5, 1-4; cycle 1-2. onTree=2, onCycle=1. answer = 5-2+3-1-3 = 2.
Query (5,3): tree 5-4-1 (toggle back OFF), cycle 1-3 (length 1). Toggle 1-3. onTree=0, onCycle=2. answer = 5-0+3-2-3 = 3.

Output:
2
3

**Test E**
Input:
3 3
1 2
2 3
3 1
1 2
2 3
1 3

Pure triangle (k=3).

Query (1,2): toggle 1-2. onCycle=1. answer = 3+3-1-3 = 2.
Query (2,3): toggle 2-3. onCycle=2. answer = 3+3-2-3 = 1.
Query (1,3): path 1-3 (length 1). Toggle 3-1. onCycle=3. answer = 3+1-3 = 1.

Output:
2
1
1