#include <iostream>
#include <vector>
using namespace std;
const int maxn=1e5+7;
int n,m;
int par[maxn],lv[maxn],f[maxn][18];
int in[maxn],out[maxn],bit[maxn],dp[maxn];
vector<int> adj[maxn],road[maxn];
struct Query{int a,b,c;}q[maxn];
inline void Dfs(const int &u,const int &p){
static int nVisit=0;
in[u]=++nVisit;
par[u]=p;
lv[u]=lv[p]+1;
f[u][0]=p;
for(int i=1;i<18;i++) f[u][i]=f[f[u][i-1]][i-1];
for(int v: adj[u]) if(v!=p) Dfs(v,u);
out[u]=nVisit;
}
inline int Jump(int u,int k){
for(int cur=0;k;k>>=1,cur++) if(k&1) u=f[u][cur];
return u;
}
inline int Lca(int u,int v){
if(lv[u]>lv[v]) swap(u,v);
v=Jump(v,lv[v]-lv[u]);
if(u==v) return u;
for(int i=17;i>=0;i--) if(f[u][i]!=f[v][i]) u=f[u][i],v=f[v][i];
return par[u];
}
inline void Update(int x,const int &val){for(;x<=n;x+=x&-x) bit[x]+=val;}
inline int Get(int x){int res=0;for(;x;x-=x&-x) res+=bit[x];return res;}
inline void Solve(const int &u,const int &p){
int sumChild=0;
for(int v: adj[u]) if(v!=p){
Solve(v,u);
sumChild+=dp[v];
}
dp[u]=sumChild;
for(int i: road[u]){
int a=q[i].a,b=q[i].b,c=q[i].c;
dp[u]=max(dp[u],sumChild+Get(in[a])+Get(in[b])+c);
}
Update(in[u],sumChild-dp[u]); Update(out[u]+1,dp[u]-sumChild);
}
inline void Read(int &x){register char c;for(c=getchar();c>'9'||c<'0';c=getchar());for(x=0;'0'<=c&&x<='9';c=getchar()) x=x*10+c-'0';}
inline void Write(const int &x){if(x>9) Write(x/10);putchar(x%10+'0');}
int main(){
Read(n);
for(int i=1;i<n;i++){
int u,v;
Read(u); Read(v);
adj[u].push_back(v);
adj[v].push_back(u);
}
Dfs(1,0);
Read(m);
for(int i=1;i<=m;i++){
Read(q[i].a);Read(q[i].b);Read(q[i].c);
road[Lca(q[i].a,q[i].b)].push_back(i);
}
Solve(1,0);
Write(dp[1]);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
5240 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5212 KB |
Output is correct |
3 |
Incorrect |
6 ms |
5112 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5212 KB |
Output is correct |
3 |
Incorrect |
6 ms |
5112 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
646 ms |
263168 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
5240 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
5240 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |