#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int, int> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 200010
#define INF (int)1e9
#define MOD 998244353
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
#define DEBUG 0
int TC,N,K;
int depth[maxn],twok[maxn][20],st[maxn],en[maxn];
vector <pii> A[maxn];
int M;
int a,b,c;
vi adj[maxn];
int co = 1;
struct FW{
int fw[maxn],N;
FW(int _N){
N = _N; FOR(i,1,N) fw[i] = 0;
}
void update(int p,int v){
for (int i=p;i<=N;i+=i&(-i)) fw[i] += v;
}
void upd(int a,int b,int c){
update(a,c); update(b+1,-c);
}
int qry(int p){
int ans = 0;
for (int i=p;i>0;i-=i&(-i)) ans += fw[i];
return ans;
}
}*fw;
void dfs(int x,int p){
twok[x][0] = p;
st[x] = co++;
aFOR(i, adj[x]) if (i != p){
depth[i] = depth[x] + 1; dfs(i,x);
}
en[x] = co - 1;
}
int kpar(int x,int k){
FOR(i,0,19) if (k &(1<<i)) x = twok[x][k];
return x;
}
int lca(int a,int b){
if (depth[a] < depth[b]) swap(a,b);
int d = depth[a] - depth[b];
FOR(i,0,19) if ((1<<i)&d) a = twok[a][i];
if (a == b) return a;
DEC(i,19,0) if (twok[a][i] != twok[b][i]){
a = twok[a][i]; b = twok[b][i];
}
return twok[a][0];
}
int dp[maxn];
void compdp(int x,int p){
aFOR(i,adj[x]) if (i != p) compdp(i,x);
int sm = 0;
aFOR(i,adj[x]) if (i != p) sm += dp[i];
dp[x] = sm;
aFOR(i,A[x]){
int a = i.f.f, b = i.f.s, c = i.s;
int res = c + sm;
if (a != x){
int ap = kpar(a,depth[a]-depth[x]-1);
res += fw->qry(st[a]) - fw->qry(st[ap]);
}
if (b != x){
int bp = kpar(b,depth[b]-depth[x]-1);
res += fw->qry(st[b]) - fw->qry(st[bp]);
}
dp[x] = max(dp[x], res);
}
fw->upd(st[x], en[x], sm - dp[x]);
}
int32_t main(){
fast;
cin >> N;
FOR(i,1,N-1){
cin >> a >> b; adj[a].pb(b); adj[b].pb(a);
}
dfs(1,-1);
FOR(k,1,19){
FOR(i,1,N){
if (twok[i][k-1] == -1) twok[i][k] = -1;
else twok[i][k] = twok[twok[i][k-1]][k-1];
}
}
cin >> M;
FOR(i,1,M){
cin >> a >> b >> c;
A[lca(a,b)].pb(pii(pi(a,b), c));
}
fw = new FW(N);
compdp(1,-1);
cout << dp[1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9768 KB |
Output is correct |
3 |
Correct |
5 ms |
9712 KB |
Output is correct |
4 |
Correct |
5 ms |
9940 KB |
Output is correct |
5 |
Correct |
97 ms |
33232 KB |
Output is correct |
6 |
Incorrect |
60 ms |
41804 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
166 ms |
36108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9768 KB |
Output is correct |
3 |
Correct |
5 ms |
9712 KB |
Output is correct |
4 |
Correct |
5 ms |
9940 KB |
Output is correct |
5 |
Correct |
97 ms |
33232 KB |
Output is correct |
6 |
Incorrect |
60 ms |
41804 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9768 KB |
Output is correct |
3 |
Correct |
5 ms |
9712 KB |
Output is correct |
4 |
Correct |
5 ms |
9940 KB |
Output is correct |
5 |
Correct |
97 ms |
33232 KB |
Output is correct |
6 |
Incorrect |
60 ms |
41804 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |