#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 1e5 + 7, LG = 20;
vector <int> g[N];
vector <int> mem[N];
int to[N][LG];
int tin[N], tout[N], timer = 0;
void dfs(int u, int p) {
to[u][0] = p;
for (int i = 1; i < LG; ++i)
to[u][i] = to[to[u][i - 1]][i - 1];
tin[u] = timer++;
for (int v : g[u])
if (v != p)
dfs(v, u);
tout[u] = timer++;
}
bool anc(int u, int v) {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int lca(int u, int v) {
if (anc(u, v))
return u;
for (int i = LG - 1; i >= 0; --i)
if (!anc(to[u][i], v))
u = to[u][i];
return to[u][0];
}
struct Quer {
int u, v, c;
} d[N];
int dp[N];
int get(int u, int p) {
int ans = 0, last = -1;
while (u != p) {
for (int v : g[u]) {
if (v != to[u][0] && v != last) {
ans += dp[v];
}
}
last = u;
u = to[u][0];
}
return ans;
}
int add(int u, int v1, int v2) {
int ans = 0;
for (int v : g[u]) {
if (!anc(v, v1) && !anc(v, v2))
ans += dp[v];
}
return ans;
}
void solve(int u, int p) {
for (int v : g[u])
if (v != p) {
solve(v, u);
dp[u] += dp[v];
}
for (int i : mem[u])
dp[u] = max(dp[u], get(d[i].u, u) + get(d[i].v, u) + add(u, d[i].u, d[i].v) + d[i].c);
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n;
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
g[u].app(v); g[v].app(u);
}
dfs(1, 1);
int m;
cin >> m;
for (int i = 0; i < m; ++i) {
cin >> d[i].u >> d[i].v >> d[i].c;
mem[lca(d[i].u, d[i].v)].app(i);
}
solve(1, 1);
#ifdef HOME
for (int i = 1; i <= n; ++i)
cout << dp[i] << ' ';
cout << '\n';
#endif
cout << dp[1] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4988 KB |
Output is correct |
2 |
Correct |
9 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
9 ms |
5240 KB |
Output is correct |
5 |
Correct |
129 ms |
28280 KB |
Output is correct |
6 |
Correct |
74 ms |
35192 KB |
Output is correct |
7 |
Correct |
150 ms |
32868 KB |
Output is correct |
8 |
Correct |
91 ms |
27512 KB |
Output is correct |
9 |
Correct |
187 ms |
31228 KB |
Output is correct |
10 |
Correct |
92 ms |
27512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
5112 KB |
Output is correct |
2 |
Correct |
9 ms |
5164 KB |
Output is correct |
3 |
Correct |
10 ms |
5368 KB |
Output is correct |
4 |
Execution timed out |
1086 ms |
40572 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
5112 KB |
Output is correct |
2 |
Correct |
9 ms |
5164 KB |
Output is correct |
3 |
Correct |
10 ms |
5368 KB |
Output is correct |
4 |
Execution timed out |
1086 ms |
40572 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
379 ms |
32728 KB |
Output is correct |
2 |
Execution timed out |
1086 ms |
40548 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4988 KB |
Output is correct |
2 |
Correct |
9 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
9 ms |
5240 KB |
Output is correct |
5 |
Correct |
129 ms |
28280 KB |
Output is correct |
6 |
Correct |
74 ms |
35192 KB |
Output is correct |
7 |
Correct |
150 ms |
32868 KB |
Output is correct |
8 |
Correct |
91 ms |
27512 KB |
Output is correct |
9 |
Correct |
187 ms |
31228 KB |
Output is correct |
10 |
Correct |
92 ms |
27512 KB |
Output is correct |
11 |
Correct |
9 ms |
5368 KB |
Output is correct |
12 |
Correct |
10 ms |
5368 KB |
Output is correct |
13 |
Correct |
10 ms |
5368 KB |
Output is correct |
14 |
Correct |
10 ms |
5368 KB |
Output is correct |
15 |
Correct |
10 ms |
5368 KB |
Output is correct |
16 |
Correct |
11 ms |
5368 KB |
Output is correct |
17 |
Correct |
10 ms |
5368 KB |
Output is correct |
18 |
Correct |
12 ms |
5368 KB |
Output is correct |
19 |
Correct |
10 ms |
5368 KB |
Output is correct |
20 |
Correct |
11 ms |
5496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4988 KB |
Output is correct |
2 |
Correct |
9 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
9 ms |
5240 KB |
Output is correct |
5 |
Correct |
129 ms |
28280 KB |
Output is correct |
6 |
Correct |
74 ms |
35192 KB |
Output is correct |
7 |
Correct |
150 ms |
32868 KB |
Output is correct |
8 |
Correct |
91 ms |
27512 KB |
Output is correct |
9 |
Correct |
187 ms |
31228 KB |
Output is correct |
10 |
Correct |
92 ms |
27512 KB |
Output is correct |
11 |
Correct |
9 ms |
5112 KB |
Output is correct |
12 |
Correct |
9 ms |
5164 KB |
Output is correct |
13 |
Correct |
10 ms |
5368 KB |
Output is correct |
14 |
Execution timed out |
1086 ms |
40572 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |