Logo
(追記) (追記ここまで)

7652번 - Bonsai 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB148866.667%

문제

After being assaulted in the parking lot by Mr. Miyagi following the “All Valley Karate Tournament”, John Kreese has come to you for assistance. Help John in his quest for justice by chopping off all the leaves from Mr. Miyagi’s bonsai tree!

You are given an undirected tree (i.e., a connected graph with no cycles), where each edge (i.e., branch) has a nonnegative weight (i.e., thickness). One vertex of the tree has been designated the root of the tree. The remaining vertices of the tree each have unique paths to the root; non-root vertices which are not the successors of any other vertex on a path to the root are known as leaves.

Determine the minimum weight set of edges that must be removed so that none of the leaves in the original tree are connected by some path to the root.

입력

The input file will contain multiple test cases. Each test case will begin with a line containing a pair of integers n (where 1 ≤ n ≤ 1000) and r (where r ∈ {1, . . . , n}) indicating the number of vertices in the tree and the index of the root vertex, respectively. The next n−1 lines each contain three integers ui vi wi (where ui, vi ∈ {1, . . . , n} and 0 ≤ wi ≤ 1000) indicating that vertex ui is connected to vertex vi by an undirected edge with weight wi. The input file will not contain duplicate edges. The end-of-file is denoted by a single line containing “0 0”.

출력

For each input test case, print a single integer indicating the minimum total weight of edges that must be deleted in order to ensure that there exists no path from one of the original leaves to the root.

제한

예제 입력 1

15 15
1 2 1
2 3 2
2 5 3
5 6 7
4 6 5
6 7 4
5 15 6
15 10 11
10 13 5
13 14 4
12 13 3
9 10 8
8 9 2
9 11 3
0 0

예제 출력 1

16

힌트

출처

University > Stanford Local ACM Programming Contest > SLPC 2009 A번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /