Solutions are presented as using the least memory and the fastest execution time. It also takes the top
10 most recent solutions from each language. If you want to limit to a specific index, click the
"Solved" button and go to that problem.
Problems
Mayor of Yusland just won the lottery and decided to spent money on something good for town. For example, repair all the roads in the town. Yusland consists of n intersections connected by n - 1 bidirectional roads. One can travel from any intersection to any other intersection using only these roads. There is only one road repairing company in town, named "RC company". Company's center is located at the intersection 1 . RC company doesn't repair roads you tell them. Instead, they have workers at some intersections, who can repair only some specific paths. The i -th worker can be paid c i coins and then he repairs all roads on a path from u i to some v i that lies on the path from u i to intersection 1 . Mayor asks you to choose the cheapest way to hire some subset of workers in order to repair all the roads in Yusland. It's allowed that some roads will be repaired more than once. If it's impossible to repair all roads print - 1 . The first line of the input contains two integers n and m ( 1 ≤ n , m ≤ 300 000 ) — the number of cities in Yusland and the number of workers respectively. Then follow n −1 line, each of them contains two integers x i and y i ( 1 ≤ x i , y i ≤ n ) — indices of intersections connected by the i -th road. Last m lines provide the description of workers, each line containing three integers u i , v i and c i ( 1 ≤ u i , v i ≤ n , 1 ≤ c i ≤ 10 9 ). This means that the i -th worker can repair all roads on the path from v i to u i for c i coins. It's guaranteed that v i lies on the path from u i to 1 . Note that v i and u i may coincide. If it's impossible to repair all roads then print - 1 . Otherwise print a single integer — minimum cost required to repair all roads using "RC company" workers. In the first sample, we should choose workers with indices 1 , 3 , 4 and 5 , some roads will be repaired more than once but it is OK. The cost will be equal to 2 + 3 + 1 + 2 = 8 coins. |
Tutorials
Submissions
Submission Id |
Author(s) |
Index |
Submitted |
Verdict |
Language |
Test Set |
Tests Passed |
Time taken (ms) |
Memory Consumed (bytes) |
Tags |
Rating |
|
29866475 |
laofudasuan |
D |
Aug. 29, 2017, 1:03 p.m. |
OK |
GNU C++ |
TESTS |
82 |
187 |
47820800 |
|
2800 |
|
33496073 |
ONION_CYC |
D |
Dec. 22, 2017, 6:13 a.m. |
OK |
GNU C++ |
TESTS |
82 |
217 |
28262400 |
|
2800 |
|
31169388 |
Thefinal |
D |
Oct. 9, 2017, 2:02 p.m. |
OK |
GNU C++ |
TESTS |
82 |
249 |
72499200 |
|
2800 |
|
18167540 |
Lightning34 |
D |
May 31, 2016, 11:21 a.m. |
OK |
GNU C++ |
TESTS |
82 |
265 |
54272000 |
|
2800 |
|
24683974 |
huanghc |
D |
Feb. 15, 2017, 1:40 a.m. |
OK |
GNU C++ |
TESTS |
82 |
389 |
49459200 |
|
2800 |
|
31137455 |
vjudge2 |
D |
Oct. 8, 2017, 8:33 a.m. |
OK |
GNU C++ |
TESTS |
82 |
405 |
63692800 |
|
2800 |
|
20548357 |
Talon_F |
D |
Sept. 12, 2016, 12:07 p.m. |
OK |
GNU C++ |
TESTS |
82 |
405 |
69836800 |
|
2800 |
|
18374136 |
akautomation |
D |
June 11, 2016, 12:09 p.m. |
OK |
GNU C++ |
TESTS |
82 |
452 |
129433600 |
|
2800 |
|
20562620 |
Hermera |
D |
Sept. 13, 2016, 8:28 a.m. |
OK |
GNU C++ |
TESTS |
82 |
467 |
69836800 |
|
2800 |
|
19038612 |
akautomation |
D |
July 12, 2016, 1:06 a.m. |
OK |
GNU C++ |
TESTS |
82 |
467 |
105164800 |
|
2800 |
|
51825081 |
luogu_bot3 |
D |
March 25, 2019, 1:41 p.m. |
OK |
GNU C++11 |
TESTS |
82 |
155 |
24678400 |
|
2800 |
|
58986305 |
luogu_bot4 |
D |
Aug. 18, 2019, 7:14 a.m. |
OK |
GNU C++11 |
TESTS |
82 |
156 |
32051200 |
|
2800 |
|
51221489 |
hyjhyj |
D |
March 12, 2019, 12:37 p.m. |
OK |
GNU C++11 |
TESTS |
82 |
156 |
35430400 |
|
2800 |
|
51844474 |
Rayment |
D |
March 26, 2019, 2:59 a.m. |
OK |
GNU C++11 |
TESTS |
82 |
171 |
24678400 |
|
2800 |
|
48474753 |
zusuyu |
D |
Jan. 17, 2019, 1:33 a.m. |
OK |
GNU C++11 |
TESTS |
82 |
171 |
24678400 |
|
2800 |
|
53115110 |
luogu_bot1 |
D |
April 22, 2019, 7:23 a.m. |
OK |
GNU C++11 |
TESTS |
82 |
171 |
26214400 |
|
2800 |
|
45733390 |
Staray |
D |
Nov. 14, 2018, 4:57 p.m. |
OK |
GNU C++11 |
TESTS |
82 |
171 |
28467200 |
|
2800 |
|
51221300 |
hyjhyj |
D |
March 12, 2019, 12:33 p.m. |
OK |
GNU C++11 |
TESTS |
82 |
171 |
34304000 |
|
2800 |
|
51488166 |
PDDNB |
D |
March 19, 2019, 2:29 p.m. |
OK |
GNU C++11 |
TESTS |
82 |
171 |
42803200 |
|
2800 |
|
51220960 |
xgzc |
D |
March 12, 2019, 12:26 p.m. |
OK |
GNU C++11 |
TESTS |
82 |
186 |
29696000 |
|
2800 |
|
66310311 |
Minamoto |
D |
Dec. 5, 2019, 11:10 a.m. |
OK |
GNU C++14 |
TESTS |
82 |
498 |
63180800 |
|
2800 |
|
66310037 |
Minamoto |
D |
Dec. 5, 2019, 11:04 a.m. |
OK |
GNU C++14 |
TESTS |
82 |
498 |
63180800 |
|
2800 |
|
64908802 |
Created_equal |
D |
Nov. 15, 2019, 1:14 a.m. |
OK |
GNU C++14 |
TESTS |
82 |
499 |
84992000 |
|
2800 |
|
65346217 |
wucstdio |
D |
Nov. 19, 2019, 12:40 p.m. |
OK |
GNU C++14 |
TESTS |
82 |
545 |
71168000 |
|
2800 |
|
64619825 |
mayaohua2003 |
D |
Nov. 10, 2019, 6:28 a.m. |
OK |
GNU C++14 |
TESTS |
82 |
592 |
47718400 |
|
2800 |
|
69574871 |
qiqi20021026 |
D |
Jan. 26, 2020, 3:13 p.m. |
OK |
GNU C++14 |
TESTS |
82 |
592 |
60211200 |
|
2800 |
|
30097453 |
LJK... |
D |
Sept. 5, 2017, 11:06 a.m. |
OK |
GNU C++14 |
TESTS |
82 |
608 |
50995200 |
|
2800 |
|
66619428 |
yhx-12243 |
D |
Dec. 11, 2019, 6:17 a.m. |
OK |
GNU C++14 |
TESTS |
82 |
623 |
67891200 |
|
2800 |
|
31860809 |
WildRage |
D |
Oct. 29, 2017, 8:49 a.m. |
OK |
GNU C++14 |
TESTS |
82 |
623 |
91750400 |
|
2800 |
|
63623845 |
zsyzsy |
D |
Oct. 28, 2019, 1:54 a.m. |
OK |
GNU C++14 |
TESTS |
82 |
624 |
24883200 |
|
2800 |
|
69911517 |
HirasawaaYui |
D |
Jan. 31, 2020, 3:31 p.m. |
OK |
GNU C++17 |
TESTS |
82 |
358 |
57036800 |
|
2800 |
|
67007942 |
little_gift |
D |
Dec. 16, 2019, 8:44 a.m. |
OK |
GNU C++17 |
TESTS |
82 |
545 |
71782400 |
|
2800 |
|
55105800 |
negiizhao |
D |
June 5, 2019, 2:21 a.m. |
OK |
GNU C++17 |
TESTS |
82 |
592 |
29900800 |
|
2800 |
|
45733495 |
vjudge4 |
D |
Nov. 14, 2018, 4:57 p.m. |
OK |
GNU C++17 |
TESTS |
82 |
608 |
28774400 |
|
2800 |
|
51488084 |
PDDNB |
D |
March 19, 2019, 2:27 p.m. |
OK |
GNU C++17 |
TESTS |
82 |
623 |
42803200 |
|
2800 |
|
58259303 |
Rzepa |
D |
Aug. 4, 2019, 11:32 a.m. |
OK |
GNU C++17 |
TESTS |
82 |
685 |
60211200 |
|
2800 |
|
60290998 |
Rose_max |
D |
Sept. 9, 2019, 7:10 a.m. |
OK |
GNU C++17 |
TESTS |
82 |
717 |
54476800 |
|
2800 |
|
55707875 |
vjudge2 |
D |
June 18, 2019, 8:38 a.m. |
OK |
GNU C++17 |
TESTS |
82 |
717 |
104448000 |
|
2800 |
|
48413917 |
pishbeenZ |
D |
Jan. 15, 2019, 9:18 a.m. |
OK |
GNU C++17 |
TESTS |
82 |
763 |
66764800 |
|
2800 |
|
60465203 |
AliShahali1382 |
D |
Sept. 13, 2019, 12:41 a.m. |
OK |
GNU C++17 |
TESTS |
82 |
764 |
48947200 |
|
2800 |
|
24091061 |
sdya |
D |
Jan. 24, 2017, 12:29 p.m. |
OK |
MS C++ |
TESTS |
82 |
701 |
57548800 |
|
2800 |
|
31860818 |
WildRage |
D |
Oct. 29, 2017, 8:50 a.m. |
OK |
MS C++ |
TESTS |
82 |
748 |
99123200 |
|
2800 |
|
31865177 |
Hzoi_joker |
D |
Oct. 29, 2017, 11:40 a.m. |
OK |
MS C++ |
TESTS |
82 |
873 |
71577600 |
|
2800 |
|
18041023 |
akaru |
D |
May 23, 2016, 8:37 a.m. |
OK |
MS C++ |
TESTS |
82 |
1028 |
49664000 |
|
2800 |
|
17916988 |
Ernest2002 |
D |
May 15, 2016, 8:29 a.m. |
OK |
MS C++ |
TESTS |
82 |
1138 |
260198400 |
|
2800 |
|
17884928 |
Auster |
D |
May 13, 2016, 7 a.m. |
OK |
MS C++ |
TESTS |
82 |
1263 |
84275200 |
|
2800 |
|
22461856 |
AleksanderBalobanov |
D |
Nov. 24, 2016, 8:28 a.m. |
OK |
MS C++ |
TESTS |
82 |
1404 |
93081600 |
|
2800 |
|
18119819 |
zhangJvv |
D |
May 27, 2016, 2:32 p.m. |
OK |
MS C++ |
TESTS |
82 |
1419 |
70553600 |
|
2800 |
remove filters
Back to search problems