아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Tree Paths

시간 제한7초메모리 제한512 MB

요약
트리에서 정점 번호가 연속 구간 a..b를 이루는 경로의 개수를 센다.
난이도

보통10점 중 6점

유형
트리, DFS, 구현
정답자
아직 제출이 없습니다

문제

There is a tree of NN vertices numbered 11 to NN. A path is a sequence of distinct vertices (v_1,…,v_k)(v\_1, \ldots, v\_k) such that k≥1k \geq 1, v_iv_i+1v\_i v\_{i+1} is an edge for all 1≤i≤k−11 \leq i \leq k-1, and v_1≤v_kv\_1 \leq v\_k.

Count the number of paths such that the vertices v_1,…,v_kv\_1, \ldots, v\_k form a contiguous range, or more formally, the set v_1,…,v_k=a,a+1,…,b\\{v\_1, \ldots, v\_k\\} = \\{a, a+1, \ldots, b\\} for some integers a≤ba \leq b.

입력

The first line contains an integer NN (1≤N≤50,0001 \leq N \leq 50\\,000). The next N−1N-1 lines contain the edges of the tree. The ii-th of these lines contains two space-separated integers u_iu\_i and v_iv\_i (1≤u_i,v_i≤N1 \leq u\_i, v\_i \leq N) denoting that u_iv_iu\_i v\_i is an edge. It is guaranteed that the given graph is a tree.

출력

On a single line output the desired number of paths.

힌트

The paths are (1)(1), (2)(2), (3)(3), (1,2)(1,2), and (2,1,3)(2,1,3).

예제1

  1. 예제 1

    입력
    3
    1 2
    1 3
    
    예상 출력
    5