#6257. [SDCPC 2019] Connected Intervals

[SDCPC 2019] Connected Intervals

[SDCPC 2019] Connected Intervals

题目描述

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

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

Recall that an induced subgraph G′G' 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 $T$ indicating the number of test cases. For each test case:

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

For the following (n−1)(n-1) lines, the ii-th line contains two integers aia_i and bib_i (1≤ai,bi≤n1 \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. 标签: P14050|2019|山东|省赛/邀请赛

来源

P14050|[SDCPC 2019] Connected Intervals