#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
vector<int> t[maxn], vec[maxn];
int dp[maxn], mx[maxn], c[maxn], lazy[maxn], cost[maxn], w[maxn];
set<int> s[maxn];
void dfs(int v, int p = -1){
int sum = 0;
for (auto u : t[v])
if (u != p)
dfs(u, v), sum += dp[u];
dp[v] = sum + mx[v];
for (auto u : t[v])
if (u != p)
lazy[c[u]] += sum - dp[u];
for (auto u : t[v]){
if (u != p){
if (s[c[u]].size() > s[c[v]].size())
swap(c[v], c[u]);
for (auto idx : s[c[u]]){
if (s[c[v]].find(idx) != s[c[v]].end()){
dp[v] = max(dp[v], cost[idx] + lazy[c[v]] + lazy[c[u]] - sum + w[idx]);
s[c[v]].erase(idx);
}
else{
s[c[v]].insert(idx);
cost[idx] += lazy[c[u]] - lazy[c[v]];
}
}
s[c[u]].clear();
}
}
for (auto idx : vec[v]){
if (s[c[v]].find(idx) == s[c[v]].end())
s[c[v]].insert(idx), cost[idx] += sum - lazy[c[v]];
else
dp[v] = max(dp[v], cost[idx] + lazy[c[v]] + w[idx]);
}
}
int main(){
ios_base::sync_with_stdio (false);
int n;
cin >> n;
for (int i = 0; i < n - 1; i++){
int v, u;
cin >> v >> u;
// v --, u --;
t[v].push_back(u);
t[u].push_back(v);
}
int m;
cin >> m;
for (int i = 1; i <= n; i++)
c[i] = i;
for (int i = 1; i <= m; i++){
int v, u;
cin >> v >> u >> w[i];
if (v == u){
mx[v] = max(mx[v], w[i]);
continue;
}
vec[v].push_back(i);
vec[u].push_back(i);
}
dfs(1);
cout << dp[1] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
10 ms |
9720 KB |
Output is correct |
4 |
Correct |
10 ms |
9848 KB |
Output is correct |
5 |
Correct |
70 ms |
14172 KB |
Output is correct |
6 |
Correct |
63 ms |
28024 KB |
Output is correct |
7 |
Correct |
78 ms |
23288 KB |
Output is correct |
8 |
Correct |
63 ms |
14712 KB |
Output is correct |
9 |
Correct |
78 ms |
20596 KB |
Output is correct |
10 |
Correct |
60 ms |
15736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
11 ms |
9976 KB |
Output is correct |
4 |
Correct |
220 ms |
35332 KB |
Output is correct |
5 |
Correct |
264 ms |
35340 KB |
Output is correct |
6 |
Correct |
242 ms |
35320 KB |
Output is correct |
7 |
Correct |
216 ms |
35324 KB |
Output is correct |
8 |
Correct |
216 ms |
35272 KB |
Output is correct |
9 |
Correct |
205 ms |
35448 KB |
Output is correct |
10 |
Correct |
217 ms |
35192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
11 ms |
9976 KB |
Output is correct |
4 |
Correct |
220 ms |
35332 KB |
Output is correct |
5 |
Correct |
264 ms |
35340 KB |
Output is correct |
6 |
Correct |
242 ms |
35320 KB |
Output is correct |
7 |
Correct |
216 ms |
35324 KB |
Output is correct |
8 |
Correct |
216 ms |
35272 KB |
Output is correct |
9 |
Correct |
205 ms |
35448 KB |
Output is correct |
10 |
Correct |
217 ms |
35192 KB |
Output is correct |
11 |
Correct |
36 ms |
11516 KB |
Output is correct |
12 |
Correct |
220 ms |
35208 KB |
Output is correct |
13 |
Correct |
218 ms |
35292 KB |
Output is correct |
14 |
Correct |
200 ms |
35192 KB |
Output is correct |
15 |
Correct |
214 ms |
35320 KB |
Output is correct |
16 |
Correct |
209 ms |
35280 KB |
Output is correct |
17 |
Correct |
223 ms |
35240 KB |
Output is correct |
18 |
Correct |
218 ms |
35192 KB |
Output is correct |
19 |
Correct |
204 ms |
35220 KB |
Output is correct |
20 |
Correct |
216 ms |
35304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
421 ms |
24592 KB |
Output is correct |
2 |
Correct |
230 ms |
37696 KB |
Output is correct |
3 |
Correct |
261 ms |
32504 KB |
Output is correct |
4 |
Correct |
291 ms |
25976 KB |
Output is correct |
5 |
Correct |
250 ms |
33196 KB |
Output is correct |
6 |
Correct |
270 ms |
25180 KB |
Output is correct |
7 |
Correct |
269 ms |
32120 KB |
Output is correct |
8 |
Correct |
352 ms |
26488 KB |
Output is correct |
9 |
Correct |
201 ms |
37752 KB |
Output is correct |
10 |
Correct |
289 ms |
31324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
10 ms |
9720 KB |
Output is correct |
4 |
Correct |
10 ms |
9848 KB |
Output is correct |
5 |
Correct |
70 ms |
14172 KB |
Output is correct |
6 |
Correct |
63 ms |
28024 KB |
Output is correct |
7 |
Correct |
78 ms |
23288 KB |
Output is correct |
8 |
Correct |
63 ms |
14712 KB |
Output is correct |
9 |
Correct |
78 ms |
20596 KB |
Output is correct |
10 |
Correct |
60 ms |
15736 KB |
Output is correct |
11 |
Correct |
12 ms |
9976 KB |
Output is correct |
12 |
Correct |
11 ms |
9976 KB |
Output is correct |
13 |
Correct |
11 ms |
9976 KB |
Output is correct |
14 |
Correct |
11 ms |
9848 KB |
Output is correct |
15 |
Correct |
12 ms |
9976 KB |
Output is correct |
16 |
Correct |
12 ms |
9980 KB |
Output is correct |
17 |
Correct |
12 ms |
9848 KB |
Output is correct |
18 |
Correct |
12 ms |
9976 KB |
Output is correct |
19 |
Correct |
12 ms |
9980 KB |
Output is correct |
20 |
Correct |
11 ms |
10104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
10 ms |
9720 KB |
Output is correct |
4 |
Correct |
10 ms |
9848 KB |
Output is correct |
5 |
Correct |
70 ms |
14172 KB |
Output is correct |
6 |
Correct |
63 ms |
28024 KB |
Output is correct |
7 |
Correct |
78 ms |
23288 KB |
Output is correct |
8 |
Correct |
63 ms |
14712 KB |
Output is correct |
9 |
Correct |
78 ms |
20596 KB |
Output is correct |
10 |
Correct |
60 ms |
15736 KB |
Output is correct |
11 |
Correct |
10 ms |
9720 KB |
Output is correct |
12 |
Correct |
10 ms |
9720 KB |
Output is correct |
13 |
Correct |
11 ms |
9976 KB |
Output is correct |
14 |
Correct |
220 ms |
35332 KB |
Output is correct |
15 |
Correct |
264 ms |
35340 KB |
Output is correct |
16 |
Correct |
242 ms |
35320 KB |
Output is correct |
17 |
Correct |
216 ms |
35324 KB |
Output is correct |
18 |
Correct |
216 ms |
35272 KB |
Output is correct |
19 |
Correct |
205 ms |
35448 KB |
Output is correct |
20 |
Correct |
217 ms |
35192 KB |
Output is correct |
21 |
Correct |
36 ms |
11516 KB |
Output is correct |
22 |
Correct |
220 ms |
35208 KB |
Output is correct |
23 |
Correct |
218 ms |
35292 KB |
Output is correct |
24 |
Correct |
200 ms |
35192 KB |
Output is correct |
25 |
Correct |
214 ms |
35320 KB |
Output is correct |
26 |
Correct |
209 ms |
35280 KB |
Output is correct |
27 |
Correct |
223 ms |
35240 KB |
Output is correct |
28 |
Correct |
218 ms |
35192 KB |
Output is correct |
29 |
Correct |
204 ms |
35220 KB |
Output is correct |
30 |
Correct |
216 ms |
35304 KB |
Output is correct |
31 |
Correct |
421 ms |
24592 KB |
Output is correct |
32 |
Correct |
230 ms |
37696 KB |
Output is correct |
33 |
Correct |
261 ms |
32504 KB |
Output is correct |
34 |
Correct |
291 ms |
25976 KB |
Output is correct |
35 |
Correct |
250 ms |
33196 KB |
Output is correct |
36 |
Correct |
270 ms |
25180 KB |
Output is correct |
37 |
Correct |
269 ms |
32120 KB |
Output is correct |
38 |
Correct |
352 ms |
26488 KB |
Output is correct |
39 |
Correct |
201 ms |
37752 KB |
Output is correct |
40 |
Correct |
289 ms |
31324 KB |
Output is correct |
41 |
Correct |
12 ms |
9976 KB |
Output is correct |
42 |
Correct |
11 ms |
9976 KB |
Output is correct |
43 |
Correct |
11 ms |
9976 KB |
Output is correct |
44 |
Correct |
11 ms |
9848 KB |
Output is correct |
45 |
Correct |
12 ms |
9976 KB |
Output is correct |
46 |
Correct |
12 ms |
9980 KB |
Output is correct |
47 |
Correct |
12 ms |
9848 KB |
Output is correct |
48 |
Correct |
12 ms |
9976 KB |
Output is correct |
49 |
Correct |
12 ms |
9980 KB |
Output is correct |
50 |
Correct |
11 ms |
10104 KB |
Output is correct |
51 |
Correct |
359 ms |
26244 KB |
Output is correct |
52 |
Correct |
230 ms |
38020 KB |
Output is correct |
53 |
Correct |
300 ms |
31608 KB |
Output is correct |
54 |
Correct |
267 ms |
25300 KB |
Output is correct |
55 |
Correct |
458 ms |
27708 KB |
Output is correct |
56 |
Correct |
225 ms |
38008 KB |
Output is correct |
57 |
Correct |
264 ms |
31676 KB |
Output is correct |
58 |
Correct |
291 ms |
25564 KB |
Output is correct |
59 |
Correct |
358 ms |
26232 KB |
Output is correct |
60 |
Correct |
224 ms |
38008 KB |
Output is correct |
61 |
Correct |
263 ms |
31736 KB |
Output is correct |
62 |
Correct |
290 ms |
25364 KB |
Output is correct |
63 |
Correct |
435 ms |
28152 KB |
Output is correct |
64 |
Correct |
228 ms |
37980 KB |
Output is correct |
65 |
Correct |
280 ms |
31992 KB |
Output is correct |
66 |
Correct |
252 ms |
24696 KB |
Output is correct |
67 |
Correct |
428 ms |
27720 KB |
Output is correct |
68 |
Correct |
221 ms |
37996 KB |
Output is correct |
69 |
Correct |
250 ms |
29816 KB |
Output is correct |
70 |
Correct |
267 ms |
23928 KB |
Output is correct |