#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 lll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
#define MAX 9223372036854775807LL
#define MIN -9223372036854775807LL
#define INF 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout.precision(10);
#define sp << " "
#define en << "\n"
#define compress(v) sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end())
ll n, m;
ll all, bll;
vector<pll> vec[300010];
priority_queue<ll> pq[300010];
ll bun[300010];
ll dp[300010];
ll cc;
void dfs(ll here, ll pa, ll pdist)
{
ll coco = 0;
for(auto &i : vec[here])
{
if(i.fi == pa)
continue;
dfs(i.fi, here, i.se);
coco++;
}
if(!coco)
{
bun[here] = ++cc;
pq[bun[here]].push(pdist);
pq[bun[here]].push(pdist);
dp[bun[here]] = 0;
return;
}
ll maxx = 0;
for(auto &i : vec[here])
{
if(i.fi == pa)
continue;
if(maxx < (ll)pq[bun[i.fi]].size())
{
maxx = (ll)pq[bun[i.fi]].size();
bun[here] = bun[i.fi];
}
}
dp[bun[here]] -= pq[bun[here]].top();
for(auto &i : vec[here])
{
if(i.fi == pa || bun[here] == bun[i.fi])
continue;
dp[bun[here]] += dp[bun[i.fi]];
dp[bun[here]] -= pq[bun[i.fi]].top();
while(!pq[bun[i.fi]].empty())
{
pq[bun[here]].push(pq[bun[i.fi]].top());
pq[bun[i.fi]].pop();
}
}
for(ll i = 1 ; i < coco ; i++)
{
dp[bun[here]] += pq[bun[here]].top();
pq[bun[here]].pop();
}
ll gap1 = pq[bun[here]].top();
pq[bun[here]].pop();
ll gap2 = pq[bun[here]].top();
pq[bun[here]].pop();
dp[bun[here]] += gap1;
pq[bun[here]].push(gap1 + pdist);
pq[bun[here]].push(gap2 + pdist);
}
int main(void)
{
fastio
cin >> n >> m;
n += m;
for(ll i = 2 ; i <= n ; i++)
{
cin >> all >> bll;
vec[i].push_back({all, bll});
vec[all].push_back({i, bll});
}
dfs(1, 0, 0);
cout << dp[bun[1]];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
9 ms |
16724 KB |
Output is correct |
6 |
Correct |
10 ms |
16724 KB |
Output is correct |
7 |
Correct |
10 ms |
16700 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16724 KB |
Output is correct |
10 |
Correct |
10 ms |
16724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16704 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16728 KB |
Output is correct |
5 |
Correct |
10 ms |
16772 KB |
Output is correct |
6 |
Correct |
9 ms |
16724 KB |
Output is correct |
7 |
Correct |
9 ms |
16696 KB |
Output is correct |
8 |
Correct |
10 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16896 KB |
Output is correct |
10 |
Correct |
9 ms |
16724 KB |
Output is correct |
11 |
Correct |
10 ms |
16764 KB |
Output is correct |
12 |
Correct |
10 ms |
16752 KB |
Output is correct |
13 |
Correct |
10 ms |
16688 KB |
Output is correct |
14 |
Correct |
10 ms |
16724 KB |
Output is correct |
15 |
Correct |
10 ms |
16724 KB |
Output is correct |
16 |
Correct |
9 ms |
16724 KB |
Output is correct |
17 |
Correct |
9 ms |
16724 KB |
Output is correct |
18 |
Correct |
10 ms |
16688 KB |
Output is correct |
19 |
Correct |
10 ms |
16724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
9 ms |
16724 KB |
Output is correct |
6 |
Correct |
10 ms |
16724 KB |
Output is correct |
7 |
Correct |
10 ms |
16700 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16724 KB |
Output is correct |
10 |
Correct |
10 ms |
16724 KB |
Output is correct |
11 |
Correct |
9 ms |
16724 KB |
Output is correct |
12 |
Correct |
9 ms |
16704 KB |
Output is correct |
13 |
Correct |
9 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16728 KB |
Output is correct |
15 |
Correct |
10 ms |
16772 KB |
Output is correct |
16 |
Correct |
9 ms |
16724 KB |
Output is correct |
17 |
Correct |
9 ms |
16696 KB |
Output is correct |
18 |
Correct |
10 ms |
16724 KB |
Output is correct |
19 |
Correct |
9 ms |
16896 KB |
Output is correct |
20 |
Correct |
9 ms |
16724 KB |
Output is correct |
21 |
Correct |
10 ms |
16764 KB |
Output is correct |
22 |
Correct |
10 ms |
16752 KB |
Output is correct |
23 |
Correct |
10 ms |
16688 KB |
Output is correct |
24 |
Correct |
10 ms |
16724 KB |
Output is correct |
25 |
Correct |
10 ms |
16724 KB |
Output is correct |
26 |
Correct |
9 ms |
16724 KB |
Output is correct |
27 |
Correct |
9 ms |
16724 KB |
Output is correct |
28 |
Correct |
10 ms |
16688 KB |
Output is correct |
29 |
Correct |
10 ms |
16724 KB |
Output is correct |
30 |
Correct |
9 ms |
16724 KB |
Output is correct |
31 |
Correct |
10 ms |
16852 KB |
Output is correct |
32 |
Correct |
10 ms |
16852 KB |
Output is correct |
33 |
Correct |
10 ms |
16912 KB |
Output is correct |
34 |
Correct |
10 ms |
17040 KB |
Output is correct |
35 |
Correct |
11 ms |
17036 KB |
Output is correct |
36 |
Correct |
11 ms |
17108 KB |
Output is correct |
37 |
Correct |
11 ms |
17224 KB |
Output is correct |
38 |
Correct |
11 ms |
17236 KB |
Output is correct |
39 |
Correct |
13 ms |
17188 KB |
Output is correct |
40 |
Correct |
12 ms |
17748 KB |
Output is correct |
41 |
Correct |
11 ms |
17748 KB |
Output is correct |
42 |
Correct |
12 ms |
17168 KB |
Output is correct |
43 |
Correct |
14 ms |
17620 KB |
Output is correct |
44 |
Correct |
12 ms |
17492 KB |
Output is correct |
45 |
Correct |
12 ms |
17492 KB |
Output is correct |
46 |
Correct |
12 ms |
17468 KB |
Output is correct |
47 |
Correct |
13 ms |
17528 KB |
Output is correct |
48 |
Correct |
12 ms |
17292 KB |
Output is correct |
49 |
Correct |
12 ms |
17372 KB |
Output is correct |
50 |
Correct |
11 ms |
17236 KB |
Output is correct |
51 |
Correct |
11 ms |
17236 KB |
Output is correct |
52 |
Correct |
12 ms |
17364 KB |
Output is correct |
53 |
Correct |
13 ms |
17492 KB |
Output is correct |
54 |
Correct |
14 ms |
17620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
9 ms |
16724 KB |
Output is correct |
6 |
Correct |
10 ms |
16724 KB |
Output is correct |
7 |
Correct |
10 ms |
16700 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16724 KB |
Output is correct |
10 |
Correct |
10 ms |
16724 KB |
Output is correct |
11 |
Correct |
9 ms |
16724 KB |
Output is correct |
12 |
Correct |
9 ms |
16704 KB |
Output is correct |
13 |
Correct |
9 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16728 KB |
Output is correct |
15 |
Correct |
10 ms |
16772 KB |
Output is correct |
16 |
Correct |
9 ms |
16724 KB |
Output is correct |
17 |
Correct |
9 ms |
16696 KB |
Output is correct |
18 |
Correct |
10 ms |
16724 KB |
Output is correct |
19 |
Correct |
9 ms |
16896 KB |
Output is correct |
20 |
Correct |
9 ms |
16724 KB |
Output is correct |
21 |
Correct |
10 ms |
16764 KB |
Output is correct |
22 |
Correct |
10 ms |
16752 KB |
Output is correct |
23 |
Correct |
10 ms |
16688 KB |
Output is correct |
24 |
Correct |
10 ms |
16724 KB |
Output is correct |
25 |
Correct |
10 ms |
16724 KB |
Output is correct |
26 |
Correct |
9 ms |
16724 KB |
Output is correct |
27 |
Correct |
9 ms |
16724 KB |
Output is correct |
28 |
Correct |
10 ms |
16688 KB |
Output is correct |
29 |
Correct |
10 ms |
16724 KB |
Output is correct |
30 |
Correct |
9 ms |
16724 KB |
Output is correct |
31 |
Correct |
10 ms |
16852 KB |
Output is correct |
32 |
Correct |
10 ms |
16852 KB |
Output is correct |
33 |
Correct |
10 ms |
16912 KB |
Output is correct |
34 |
Correct |
10 ms |
17040 KB |
Output is correct |
35 |
Correct |
11 ms |
17036 KB |
Output is correct |
36 |
Correct |
11 ms |
17108 KB |
Output is correct |
37 |
Correct |
11 ms |
17224 KB |
Output is correct |
38 |
Correct |
11 ms |
17236 KB |
Output is correct |
39 |
Correct |
13 ms |
17188 KB |
Output is correct |
40 |
Correct |
12 ms |
17748 KB |
Output is correct |
41 |
Correct |
11 ms |
17748 KB |
Output is correct |
42 |
Correct |
12 ms |
17168 KB |
Output is correct |
43 |
Correct |
14 ms |
17620 KB |
Output is correct |
44 |
Correct |
12 ms |
17492 KB |
Output is correct |
45 |
Correct |
12 ms |
17492 KB |
Output is correct |
46 |
Correct |
12 ms |
17468 KB |
Output is correct |
47 |
Correct |
13 ms |
17528 KB |
Output is correct |
48 |
Correct |
12 ms |
17292 KB |
Output is correct |
49 |
Correct |
12 ms |
17372 KB |
Output is correct |
50 |
Correct |
11 ms |
17236 KB |
Output is correct |
51 |
Correct |
11 ms |
17236 KB |
Output is correct |
52 |
Correct |
12 ms |
17364 KB |
Output is correct |
53 |
Correct |
13 ms |
17492 KB |
Output is correct |
54 |
Correct |
14 ms |
17620 KB |
Output is correct |
55 |
Correct |
16 ms |
18132 KB |
Output is correct |
56 |
Correct |
39 ms |
21900 KB |
Output is correct |
57 |
Correct |
68 ms |
25460 KB |
Output is correct |
58 |
Correct |
82 ms |
27908 KB |
Output is correct |
59 |
Correct |
121 ms |
31584 KB |
Output is correct |
60 |
Correct |
178 ms |
35256 KB |
Output is correct |
61 |
Correct |
182 ms |
38316 KB |
Output is correct |
62 |
Correct |
201 ms |
40344 KB |
Output is correct |
63 |
Correct |
246 ms |
44764 KB |
Output is correct |
64 |
Correct |
253 ms |
46152 KB |
Output is correct |
65 |
Correct |
137 ms |
80292 KB |
Output is correct |
66 |
Correct |
127 ms |
80260 KB |
Output is correct |
67 |
Correct |
126 ms |
38244 KB |
Output is correct |
68 |
Correct |
180 ms |
68464 KB |
Output is correct |
69 |
Correct |
223 ms |
65172 KB |
Output is correct |
70 |
Correct |
221 ms |
65008 KB |
Output is correct |
71 |
Correct |
207 ms |
64144 KB |
Output is correct |
72 |
Correct |
199 ms |
64300 KB |
Output is correct |
73 |
Correct |
179 ms |
60232 KB |
Output is correct |
74 |
Correct |
180 ms |
60412 KB |
Output is correct |
75 |
Correct |
176 ms |
58456 KB |
Output is correct |
76 |
Correct |
173 ms |
58460 KB |
Output is correct |
77 |
Correct |
198 ms |
56704 KB |
Output is correct |
78 |
Correct |
184 ms |
54392 KB |
Output is correct |
79 |
Correct |
165 ms |
60648 KB |
Output is correct |