#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 5e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
struct FenwickTree{
int n;
vector<int> bit;
FenwickTree(){}
FenwickTree(int _n) {
n = _n;
bit.resize(n + 7, 0);
}
void update(int pos, int val)
{
for(; pos <= n; pos += pos & -pos) bit[pos] += val;
}
int getsum(int pos)
{
int res = 0;
for(; pos > 0; pos -= pos & -pos) res += bit[pos];
return res;
}
int getsum(int l, int r)
{
return getsum(r) - getsum(l - 1);
}
} ft;
struct Query{
int u, v, c;
};
int n;
int m;
int dp[mxN];
int sum[mxN];
int sta[mxN];
int fin[mxN];
int dep[mxN];
int par[mxN][LOG + 1];
vector<int> adj[mxN];
vector<Query> query[mxN];
int timer = 0;
void dfs_pre(int u, int p)
{
sta[u] = ++timer;
for(int v : adj[u]) {
if(v == p) continue;
dep[v] = dep[u] + 1;
par[v][0] = u;
dfs_pre(v, u);
}
fin[u] = timer;
}
int getlca(int u, int v)
{
if(dep[u] < dep[v]) swap(u, v);
for(int j = LOG; j >= 0; j--) if(dep[par[u][j]] >= dep[v]) u = par[u][j];
if(u == v) return u;
for(int j = LOG; j >= 0; j--) {
if(par[u][j] != par[v][j]) {
u = par[u][j];
v = par[v][j];
}
}
return par[u][0];
}
void dfs(int u, int p)
{
vector<pair<int, int>> val;
int ans = 0;
for(int v : adj[u]) {
if(v == p) continue;
dfs(v, u);
ans += dp[v];
val.pb({sta[v], dp[v]});
}
sum[u] = ans;
dp[u] = ans;
sort(all(val));
for(auto it : query[u]) {
int newval = ans + it.c + ft.getsum(sta[it.u]) + ft.getsum(sta[it.v]);
// int x = it.u;
// int y = it.v;
// while(x != u) {
// newval += sum[x] - dp[x];
// x = par[x][0];
// }
// while(y != u) {
// newval += sum[y] - dp[y];
// y = par[y][0];
// }
// int newval = ans + it.c;
// if(it.u != u) {
// int p = upper_bound(all(val), make_pair(sta[it.u], inf)) - val.begin();
// newval -= val[p - 1].se;
// newval += sum[it.u];
// }
// if(it.v != u) {
// int p = upper_bound(all(val), make_pair(sta[it.v], inf)) - val.begin();
// newval -= val[p - 1].se;
// newval += sum[it.v];
// }
maximize(dp[u], newval);
}
ft.update(sta[u], sum[u] - dp[u]);
ft.update(fin[u] + 1, dp[u] - sum[u]);
}
void solve()
{
cin >> n;
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
par[1][0] = 1;
dfs_pre(1, -1);
for(int j = 1; j <= LOG; j++)
for(int i = 1; i <= n; i++)
par[i][j] = par[par[i][j - 1]][j - 1];
cin >> m;
for(int i = 1; i <= m; i++) {
int u, v, c;
cin >> u >> v >> c;
int lca = getlca(u, v);
query[lca].pb({u, v, c});
}
ft = FenwickTree(n);
dfs(1, -1);
int res = 0;
for(int i = 1; i <= n; i++) maximize(res, dp[i]);
cout << res;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen(TASK".inp" , "r" , stdin);
//freopen(TASK".out" , "w" , stdout);
int tc = 1;
//cin >> tc;
while(tc--) {
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
14 ms |
23892 KB |
Output is correct |
5 |
Correct |
91 ms |
37768 KB |
Output is correct |
6 |
Correct |
68 ms |
53184 KB |
Output is correct |
7 |
Correct |
112 ms |
47728 KB |
Output is correct |
8 |
Correct |
70 ms |
38264 KB |
Output is correct |
9 |
Correct |
81 ms |
44940 KB |
Output is correct |
10 |
Correct |
78 ms |
38272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23844 KB |
Output is correct |
2 |
Correct |
13 ms |
23856 KB |
Output is correct |
3 |
Correct |
14 ms |
24104 KB |
Output is correct |
4 |
Correct |
150 ms |
55164 KB |
Output is correct |
5 |
Correct |
150 ms |
57564 KB |
Output is correct |
6 |
Correct |
126 ms |
57624 KB |
Output is correct |
7 |
Correct |
141 ms |
57548 KB |
Output is correct |
8 |
Correct |
166 ms |
57708 KB |
Output is correct |
9 |
Correct |
139 ms |
57576 KB |
Output is correct |
10 |
Correct |
143 ms |
57660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23844 KB |
Output is correct |
2 |
Correct |
13 ms |
23856 KB |
Output is correct |
3 |
Correct |
14 ms |
24104 KB |
Output is correct |
4 |
Correct |
150 ms |
55164 KB |
Output is correct |
5 |
Correct |
150 ms |
57564 KB |
Output is correct |
6 |
Correct |
126 ms |
57624 KB |
Output is correct |
7 |
Correct |
141 ms |
57548 KB |
Output is correct |
8 |
Correct |
166 ms |
57708 KB |
Output is correct |
9 |
Correct |
139 ms |
57576 KB |
Output is correct |
10 |
Correct |
143 ms |
57660 KB |
Output is correct |
11 |
Correct |
25 ms |
24908 KB |
Output is correct |
12 |
Correct |
180 ms |
57832 KB |
Output is correct |
13 |
Correct |
135 ms |
57928 KB |
Output is correct |
14 |
Correct |
132 ms |
57932 KB |
Output is correct |
15 |
Correct |
150 ms |
57928 KB |
Output is correct |
16 |
Correct |
138 ms |
57904 KB |
Output is correct |
17 |
Correct |
135 ms |
57896 KB |
Output is correct |
18 |
Correct |
143 ms |
57872 KB |
Output is correct |
19 |
Correct |
174 ms |
57932 KB |
Output is correct |
20 |
Correct |
144 ms |
57932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
169 ms |
39260 KB |
Output is correct |
2 |
Correct |
129 ms |
55200 KB |
Output is correct |
3 |
Correct |
199 ms |
52044 KB |
Output is correct |
4 |
Correct |
139 ms |
42116 KB |
Output is correct |
5 |
Correct |
191 ms |
50924 KB |
Output is correct |
6 |
Correct |
115 ms |
42008 KB |
Output is correct |
7 |
Correct |
213 ms |
50664 KB |
Output is correct |
8 |
Correct |
175 ms |
41916 KB |
Output is correct |
9 |
Correct |
118 ms |
57676 KB |
Output is correct |
10 |
Correct |
198 ms |
48540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
14 ms |
23892 KB |
Output is correct |
5 |
Correct |
91 ms |
37768 KB |
Output is correct |
6 |
Correct |
68 ms |
53184 KB |
Output is correct |
7 |
Correct |
112 ms |
47728 KB |
Output is correct |
8 |
Correct |
70 ms |
38264 KB |
Output is correct |
9 |
Correct |
81 ms |
44940 KB |
Output is correct |
10 |
Correct |
78 ms |
38272 KB |
Output is correct |
11 |
Correct |
15 ms |
23892 KB |
Output is correct |
12 |
Correct |
14 ms |
24148 KB |
Output is correct |
13 |
Correct |
13 ms |
24040 KB |
Output is correct |
14 |
Correct |
14 ms |
24020 KB |
Output is correct |
15 |
Correct |
14 ms |
24000 KB |
Output is correct |
16 |
Correct |
13 ms |
24020 KB |
Output is correct |
17 |
Correct |
13 ms |
24028 KB |
Output is correct |
18 |
Correct |
13 ms |
24020 KB |
Output is correct |
19 |
Correct |
13 ms |
24020 KB |
Output is correct |
20 |
Correct |
13 ms |
24148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
14 ms |
23892 KB |
Output is correct |
5 |
Correct |
91 ms |
37768 KB |
Output is correct |
6 |
Correct |
68 ms |
53184 KB |
Output is correct |
7 |
Correct |
112 ms |
47728 KB |
Output is correct |
8 |
Correct |
70 ms |
38264 KB |
Output is correct |
9 |
Correct |
81 ms |
44940 KB |
Output is correct |
10 |
Correct |
78 ms |
38272 KB |
Output is correct |
11 |
Correct |
15 ms |
23844 KB |
Output is correct |
12 |
Correct |
13 ms |
23856 KB |
Output is correct |
13 |
Correct |
14 ms |
24104 KB |
Output is correct |
14 |
Correct |
150 ms |
55164 KB |
Output is correct |
15 |
Correct |
150 ms |
57564 KB |
Output is correct |
16 |
Correct |
126 ms |
57624 KB |
Output is correct |
17 |
Correct |
141 ms |
57548 KB |
Output is correct |
18 |
Correct |
166 ms |
57708 KB |
Output is correct |
19 |
Correct |
139 ms |
57576 KB |
Output is correct |
20 |
Correct |
143 ms |
57660 KB |
Output is correct |
21 |
Correct |
25 ms |
24908 KB |
Output is correct |
22 |
Correct |
180 ms |
57832 KB |
Output is correct |
23 |
Correct |
135 ms |
57928 KB |
Output is correct |
24 |
Correct |
132 ms |
57932 KB |
Output is correct |
25 |
Correct |
150 ms |
57928 KB |
Output is correct |
26 |
Correct |
138 ms |
57904 KB |
Output is correct |
27 |
Correct |
135 ms |
57896 KB |
Output is correct |
28 |
Correct |
143 ms |
57872 KB |
Output is correct |
29 |
Correct |
174 ms |
57932 KB |
Output is correct |
30 |
Correct |
144 ms |
57932 KB |
Output is correct |
31 |
Correct |
169 ms |
39260 KB |
Output is correct |
32 |
Correct |
129 ms |
55200 KB |
Output is correct |
33 |
Correct |
199 ms |
52044 KB |
Output is correct |
34 |
Correct |
139 ms |
42116 KB |
Output is correct |
35 |
Correct |
191 ms |
50924 KB |
Output is correct |
36 |
Correct |
115 ms |
42008 KB |
Output is correct |
37 |
Correct |
213 ms |
50664 KB |
Output is correct |
38 |
Correct |
175 ms |
41916 KB |
Output is correct |
39 |
Correct |
118 ms |
57676 KB |
Output is correct |
40 |
Correct |
198 ms |
48540 KB |
Output is correct |
41 |
Correct |
15 ms |
23892 KB |
Output is correct |
42 |
Correct |
14 ms |
24148 KB |
Output is correct |
43 |
Correct |
13 ms |
24040 KB |
Output is correct |
44 |
Correct |
14 ms |
24020 KB |
Output is correct |
45 |
Correct |
14 ms |
24000 KB |
Output is correct |
46 |
Correct |
13 ms |
24020 KB |
Output is correct |
47 |
Correct |
13 ms |
24028 KB |
Output is correct |
48 |
Correct |
13 ms |
24020 KB |
Output is correct |
49 |
Correct |
13 ms |
24020 KB |
Output is correct |
50 |
Correct |
13 ms |
24148 KB |
Output is correct |
51 |
Correct |
185 ms |
42216 KB |
Output is correct |
52 |
Correct |
129 ms |
57904 KB |
Output is correct |
53 |
Correct |
228 ms |
49112 KB |
Output is correct |
54 |
Correct |
139 ms |
42456 KB |
Output is correct |
55 |
Correct |
159 ms |
41960 KB |
Output is correct |
56 |
Correct |
132 ms |
57888 KB |
Output is correct |
57 |
Correct |
213 ms |
49996 KB |
Output is correct |
58 |
Correct |
116 ms |
42348 KB |
Output is correct |
59 |
Correct |
163 ms |
42084 KB |
Output is correct |
60 |
Correct |
167 ms |
57920 KB |
Output is correct |
61 |
Correct |
213 ms |
50380 KB |
Output is correct |
62 |
Correct |
125 ms |
42192 KB |
Output is correct |
63 |
Correct |
159 ms |
41708 KB |
Output is correct |
64 |
Correct |
138 ms |
57860 KB |
Output is correct |
65 |
Correct |
221 ms |
50076 KB |
Output is correct |
66 |
Correct |
136 ms |
42448 KB |
Output is correct |
67 |
Correct |
162 ms |
41860 KB |
Output is correct |
68 |
Correct |
151 ms |
57920 KB |
Output is correct |
69 |
Correct |
210 ms |
47848 KB |
Output is correct |
70 |
Correct |
137 ms |
42444 KB |
Output is correct |