#P8531. [Ynoi2003] 戌亥彗星

[Ynoi2003] 戌亥彗星

Problem Description

Define a graph as a “sea urchin” if it satisfies all of the following conditions:

  1. It is a connected graph.
  2. It contains exactly one simple cycle.
  3. For every vertex not on the cycle, its degree is at most 22.

There is a graph with nn vertices and nn edges. The ii-th edge connects uiu_i and viv_i (the graph is not guaranteed to be connected).

Define the interval subgraph [l,r][l,r] of a graph as (V,E)(V',E'), where E={(ui,vi)lir}E'=\{(u_i,v_i)\mid l\leq i\leq r\}, $V'=\{u_i\mid l\leq i\leq r\} \cup \{v_i\mid l\leq i\leq r\}$. That is, this interval subgraph contains exactly the edges in the interval and all vertices that appear in at least one edge in the interval.

Now there are qq queries. Each query gives an interval l,rl,r. For this l,rl,r, compute how many sub-intervals [l,r] (llrr)[l',r']\ (l\leq l'\leq r'\leq r) satisfy that the interval subgraph [l,r][l',r'] of the original graph is a sea urchin.

Additional notes for Condition 2:

  1. A simple cycle means: you can start from any vertex, traverse some non-repeated edges, and return to the start vertex; you must traverse at least one edge; and you do not visit any repeated vertex except that the start and end vertex are the same. For example, 12311-2-3-1 is a simple cycle. Two parallel edges 121-2 also form a simple cycle. But a graph with only one vertex is not. Also, 12345311-2-3-4-5-3-1 is not a simple cycle. (Here the cycle is written as a path starting from some vertex.)
  2. Condition 22 also requires that, besides the cycle edges, there is no edge whose both endpoints are on this cycle.

Input Format

The first line contains an integer nn, meaning the number of vertices and the number of edges.

The next nn lines each contain two integers ui,viu_i,v_i, describing an edge.

The next line contains an integer qq.

The next qq lines each contain two integers l,rl,r, describing a query.

Output Format

Output qq lines, each containing one number, the answer to the corresponding query.

10
1 3
3 4
1 2
5 2
5 6
2 6
3 4
2 9
2 1
3 1
5
1 10
9 10
3 10
1 7
4 9
4
0
3
3
1

Hint

Idea: lk&nzhtl1477, Solution: lk&ccz181078, Code: lk&ccz181078, Data: lk&ccz181078.

The testdata guarantees that there are no self-loops.

Subtask ID nn\le qq\le Special Property Score
11 100100 None 55
22 500500 1515
33 50005000
44 5000050000
55 10610^6 11
66 10610^6 The original graph is a sea urchin
77 None 2020

Translated by ChatGPT 5