#P14050. [SDCPC 2019] Connected Intervals

[SDCPC 2019] Connected Intervals

题目描述

DreamGrid has just found a tree of nn vertices in his backyard. As DreamGrid loves connected components, he defines an interval [l,r][l, r] (1lrn1 \le l \le r \le n) as a connected interval if the induced subgraph formed from the set V={vii[l,r]}\mathbb{V} = \{v_i | i \in [l, r]\} consists of exactly one connected component, where viv_i indicates the vertex whose index is ii.

Given the tree in DreamGrid's backyard, your task is to help DreamGrid count the number of connected intervals.

Recall that an induced subgraph GG' of a graph GG is another graph, formed from a subset V\mathbb{V} of the vertices of the graph GG and all of the edges in graph GG connecting pairs of vertices in V\mathbb{V}.

输入格式

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains an integer nn (1n3×1051 \le n \le 3 \times 10^5) indicating the size of the tree.

For the following (n1)(n-1) lines, the ii-th line contains two integers aia_i and bib_i (1ai,bin1 \le a_i, b_i \le n) indicating that there is an edge connecting vertex aia_i and vertex bib_i in the tree.

It's guaranteed that the given graph is a tree and that the sum of nn in all test cases will not exceed 3×1053 \times 10^5.

输出格式

For each test case output one line containing one integer, indicating the number of connected intervals.

2
4
1 2
2 3
3 4
4
1 2
2 3
2 4
10
9

提示

For the first sample test case, all intervals are connected intervals.

For the second sample test case, all intervals but [3,4][3, 4] are connected intervals.