#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, m;
cin >> n >> m;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
ed[a].pb(b);
ed[b].pb(a);
}
dfs(1, 1);
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 |
Execution timed out |
1101 ms |
242932 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
17244 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
17244 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
93 ms |
36108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
17244 KB |
Output is correct |
2 |
Execution timed out |
1101 ms |
242932 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
17244 KB |
Output is correct |
2 |
Execution timed out |
1101 ms |
242932 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |