#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 2e5 + 100;
vector <int> ed[N];
int in[N], ou[N], sp[N][20], timer = 0;
void dfs(int u, int v) {
if (u != 1) {
auto it = find(ed[u].begin(), ed[u].end(), v);
if (it != end(ed[u]))
ed[u].erase(it);
}
sp[u][0] = v;
for (int i = 1; i < 20; i++)
sp[u][i] = sp[sp[u][i - 1]][i - 1];
timer++;
in[u] = timer;
for (auto x : ed[u]) {
dfs(x, u);
}
ou[u] = timer;
}
bool is(int a, int b) {
return in[a] <= in[b] && ou[a] >= ou[b];
}
int anc(int a, int b) {
if (is(a, b))
return a;
if (is(b, a))
return b;
for (int i = 19; i >= 0; i--) {
if (is(sp[a][i], b) == 0)
a = sp[a][i];
}
return sp[a][0];
}
ll fen[N];
void upd(int u, ll x) {
for (; u < N; u += u & (-u))
fen[u] += x;
}
ll get(int u) {
ll s = 0;
for (; u; u -= u & (-u))
s += fen[u];
return s;
}
vector < vector <ll>> save[N];
ll f[N], s[N];
ll get(int u, int c) {
if (u == c)
return 0;
int l = 0, r = sz(ed[u]) - 1, ans = 0;
while (l <= r) {
int m = (l + r) / 2;
if (in[ed[u][m]] <= in[c]) {
ans = m;
l = m + 1;
} else {
r = m - 1;
}
}
return f[ed[u][ans]] - s[c];
}
void dfs2(int u) {
for (auto x : ed[u]) {
dfs2(x);
s[u] += f[x];
}
f[u] = s[u];
sort(all(ed[u]), [&](int a, int b) {return in[a] < in[b];});
for (auto x : save[u]) {
f[u] = max(f[u], x[2] + get(in[x[0]]) + get(in[x[1]]) - get(u, x[0]) - get(u, x[1]) + s[u]);
}
for (auto x : ed[u]) {
upd(in[x], s[u] - f[x]);
upd(ou[x] + 1, f[x] - s[u]);
}
}
ll solve() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
ed[a].pb(b);
ed[b].pb(a);
}
dfs(1, 1);
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
int x = anc(a, b);
save[x].pb({a, b, c});
}
dfs2(1);
return f[1];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) {
//solve();
cout << solve() << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
17244 KB |
Output is correct |
2 |
Correct |
3 ms |
17244 KB |
Output is correct |
3 |
Correct |
3 ms |
17244 KB |
Output is correct |
4 |
Correct |
4 ms |
17244 KB |
Output is correct |
5 |
Correct |
49 ms |
30052 KB |
Output is correct |
6 |
Correct |
43 ms |
40020 KB |
Output is correct |
7 |
Correct |
63 ms |
36108 KB |
Output is correct |
8 |
Correct |
42 ms |
28764 KB |
Output is correct |
9 |
Correct |
57 ms |
33876 KB |
Output is correct |
10 |
Correct |
47 ms |
28696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
17240 KB |
Output is correct |
2 |
Correct |
4 ms |
17496 KB |
Output is correct |
3 |
Correct |
4 ms |
17244 KB |
Output is correct |
4 |
Correct |
83 ms |
47820 KB |
Output is correct |
5 |
Correct |
85 ms |
47700 KB |
Output is correct |
6 |
Correct |
82 ms |
47828 KB |
Output is correct |
7 |
Correct |
88 ms |
47660 KB |
Output is correct |
8 |
Correct |
98 ms |
47700 KB |
Output is correct |
9 |
Correct |
89 ms |
47996 KB |
Output is correct |
10 |
Correct |
103 ms |
47748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
17240 KB |
Output is correct |
2 |
Correct |
4 ms |
17496 KB |
Output is correct |
3 |
Correct |
4 ms |
17244 KB |
Output is correct |
4 |
Correct |
83 ms |
47820 KB |
Output is correct |
5 |
Correct |
85 ms |
47700 KB |
Output is correct |
6 |
Correct |
82 ms |
47828 KB |
Output is correct |
7 |
Correct |
88 ms |
47660 KB |
Output is correct |
8 |
Correct |
98 ms |
47700 KB |
Output is correct |
9 |
Correct |
89 ms |
47996 KB |
Output is correct |
10 |
Correct |
103 ms |
47748 KB |
Output is correct |
11 |
Correct |
11 ms |
19032 KB |
Output is correct |
12 |
Correct |
92 ms |
47956 KB |
Output is correct |
13 |
Correct |
87 ms |
48096 KB |
Output is correct |
14 |
Correct |
87 ms |
48128 KB |
Output is correct |
15 |
Correct |
84 ms |
47992 KB |
Output is correct |
16 |
Correct |
82 ms |
48124 KB |
Output is correct |
17 |
Correct |
89 ms |
47952 KB |
Output is correct |
18 |
Correct |
88 ms |
48092 KB |
Output is correct |
19 |
Correct |
95 ms |
48224 KB |
Output is correct |
20 |
Correct |
84 ms |
47956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
35964 KB |
Output is correct |
2 |
Correct |
89 ms |
47700 KB |
Output is correct |
3 |
Correct |
168 ms |
43736 KB |
Output is correct |
4 |
Correct |
93 ms |
35664 KB |
Output is correct |
5 |
Correct |
113 ms |
43524 KB |
Output is correct |
6 |
Correct |
102 ms |
35772 KB |
Output is correct |
7 |
Correct |
140 ms |
42328 KB |
Output is correct |
8 |
Correct |
114 ms |
36976 KB |
Output is correct |
9 |
Correct |
84 ms |
47692 KB |
Output is correct |
10 |
Correct |
147 ms |
40956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
17244 KB |
Output is correct |
2 |
Correct |
3 ms |
17244 KB |
Output is correct |
3 |
Correct |
3 ms |
17244 KB |
Output is correct |
4 |
Correct |
4 ms |
17244 KB |
Output is correct |
5 |
Correct |
49 ms |
30052 KB |
Output is correct |
6 |
Correct |
43 ms |
40020 KB |
Output is correct |
7 |
Correct |
63 ms |
36108 KB |
Output is correct |
8 |
Correct |
42 ms |
28764 KB |
Output is correct |
9 |
Correct |
57 ms |
33876 KB |
Output is correct |
10 |
Correct |
47 ms |
28696 KB |
Output is correct |
11 |
Correct |
4 ms |
17244 KB |
Output is correct |
12 |
Correct |
4 ms |
17244 KB |
Output is correct |
13 |
Correct |
4 ms |
17240 KB |
Output is correct |
14 |
Correct |
5 ms |
17256 KB |
Output is correct |
15 |
Correct |
4 ms |
17440 KB |
Output is correct |
16 |
Correct |
4 ms |
17244 KB |
Output is correct |
17 |
Correct |
4 ms |
17244 KB |
Output is correct |
18 |
Correct |
4 ms |
17240 KB |
Output is correct |
19 |
Correct |
4 ms |
17244 KB |
Output is correct |
20 |
Correct |
4 ms |
17244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
17244 KB |
Output is correct |
2 |
Correct |
3 ms |
17244 KB |
Output is correct |
3 |
Correct |
3 ms |
17244 KB |
Output is correct |
4 |
Correct |
4 ms |
17244 KB |
Output is correct |
5 |
Correct |
49 ms |
30052 KB |
Output is correct |
6 |
Correct |
43 ms |
40020 KB |
Output is correct |
7 |
Correct |
63 ms |
36108 KB |
Output is correct |
8 |
Correct |
42 ms |
28764 KB |
Output is correct |
9 |
Correct |
57 ms |
33876 KB |
Output is correct |
10 |
Correct |
47 ms |
28696 KB |
Output is correct |
11 |
Correct |
3 ms |
17240 KB |
Output is correct |
12 |
Correct |
4 ms |
17496 KB |
Output is correct |
13 |
Correct |
4 ms |
17244 KB |
Output is correct |
14 |
Correct |
83 ms |
47820 KB |
Output is correct |
15 |
Correct |
85 ms |
47700 KB |
Output is correct |
16 |
Correct |
82 ms |
47828 KB |
Output is correct |
17 |
Correct |
88 ms |
47660 KB |
Output is correct |
18 |
Correct |
98 ms |
47700 KB |
Output is correct |
19 |
Correct |
89 ms |
47996 KB |
Output is correct |
20 |
Correct |
103 ms |
47748 KB |
Output is correct |
21 |
Correct |
11 ms |
19032 KB |
Output is correct |
22 |
Correct |
92 ms |
47956 KB |
Output is correct |
23 |
Correct |
87 ms |
48096 KB |
Output is correct |
24 |
Correct |
87 ms |
48128 KB |
Output is correct |
25 |
Correct |
84 ms |
47992 KB |
Output is correct |
26 |
Correct |
82 ms |
48124 KB |
Output is correct |
27 |
Correct |
89 ms |
47952 KB |
Output is correct |
28 |
Correct |
88 ms |
48092 KB |
Output is correct |
29 |
Correct |
95 ms |
48224 KB |
Output is correct |
30 |
Correct |
84 ms |
47956 KB |
Output is correct |
31 |
Correct |
105 ms |
35964 KB |
Output is correct |
32 |
Correct |
89 ms |
47700 KB |
Output is correct |
33 |
Correct |
168 ms |
43736 KB |
Output is correct |
34 |
Correct |
93 ms |
35664 KB |
Output is correct |
35 |
Correct |
113 ms |
43524 KB |
Output is correct |
36 |
Correct |
102 ms |
35772 KB |
Output is correct |
37 |
Correct |
140 ms |
42328 KB |
Output is correct |
38 |
Correct |
114 ms |
36976 KB |
Output is correct |
39 |
Correct |
84 ms |
47692 KB |
Output is correct |
40 |
Correct |
147 ms |
40956 KB |
Output is correct |
41 |
Correct |
4 ms |
17244 KB |
Output is correct |
42 |
Correct |
4 ms |
17244 KB |
Output is correct |
43 |
Correct |
4 ms |
17240 KB |
Output is correct |
44 |
Correct |
5 ms |
17256 KB |
Output is correct |
45 |
Correct |
4 ms |
17440 KB |
Output is correct |
46 |
Correct |
4 ms |
17244 KB |
Output is correct |
47 |
Correct |
4 ms |
17244 KB |
Output is correct |
48 |
Correct |
4 ms |
17240 KB |
Output is correct |
49 |
Correct |
4 ms |
17244 KB |
Output is correct |
50 |
Correct |
4 ms |
17244 KB |
Output is correct |
51 |
Correct |
112 ms |
36824 KB |
Output is correct |
52 |
Correct |
93 ms |
48568 KB |
Output is correct |
53 |
Correct |
130 ms |
41460 KB |
Output is correct |
54 |
Correct |
102 ms |
36032 KB |
Output is correct |
55 |
Correct |
102 ms |
36392 KB |
Output is correct |
56 |
Correct |
87 ms |
48092 KB |
Output is correct |
57 |
Correct |
112 ms |
42332 KB |
Output is correct |
58 |
Correct |
107 ms |
35820 KB |
Output is correct |
59 |
Correct |
105 ms |
37800 KB |
Output is correct |
60 |
Correct |
96 ms |
47952 KB |
Output is correct |
61 |
Correct |
116 ms |
42404 KB |
Output is correct |
62 |
Correct |
114 ms |
35932 KB |
Output is correct |
63 |
Correct |
107 ms |
37052 KB |
Output is correct |
64 |
Correct |
88 ms |
48040 KB |
Output is correct |
65 |
Correct |
124 ms |
42248 KB |
Output is correct |
66 |
Correct |
88 ms |
35976 KB |
Output is correct |
67 |
Correct |
100 ms |
36284 KB |
Output is correct |
68 |
Correct |
87 ms |
48240 KB |
Output is correct |
69 |
Correct |
115 ms |
40848 KB |
Output is correct |
70 |
Correct |
100 ms |
35924 KB |
Output is correct |