#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&&c<='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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
5 ms |
4984 KB |
Output is correct |
4 |
Correct |
6 ms |
5240 KB |
Output is correct |
5 |
Correct |
126 ms |
17692 KB |
Output is correct |
6 |
Correct |
48 ms |
22648 KB |
Output is correct |
7 |
Correct |
104 ms |
21084 KB |
Output is correct |
8 |
Correct |
87 ms |
18172 KB |
Output is correct |
9 |
Correct |
106 ms |
19960 KB |
Output is correct |
10 |
Correct |
94 ms |
18184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4984 KB |
Output is correct |
2 |
Correct |
5 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
5240 KB |
Output is correct |
4 |
Correct |
98 ms |
25080 KB |
Output is correct |
5 |
Correct |
106 ms |
25060 KB |
Output is correct |
6 |
Correct |
78 ms |
24960 KB |
Output is correct |
7 |
Correct |
93 ms |
24952 KB |
Output is correct |
8 |
Correct |
89 ms |
25080 KB |
Output is correct |
9 |
Correct |
86 ms |
25080 KB |
Output is correct |
10 |
Correct |
87 ms |
24952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4984 KB |
Output is correct |
2 |
Correct |
5 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
5240 KB |
Output is correct |
4 |
Correct |
98 ms |
25080 KB |
Output is correct |
5 |
Correct |
106 ms |
25060 KB |
Output is correct |
6 |
Correct |
78 ms |
24960 KB |
Output is correct |
7 |
Correct |
93 ms |
24952 KB |
Output is correct |
8 |
Correct |
89 ms |
25080 KB |
Output is correct |
9 |
Correct |
86 ms |
25080 KB |
Output is correct |
10 |
Correct |
87 ms |
24952 KB |
Output is correct |
11 |
Correct |
12 ms |
5880 KB |
Output is correct |
12 |
Correct |
98 ms |
24924 KB |
Output is correct |
13 |
Correct |
106 ms |
24980 KB |
Output is correct |
14 |
Correct |
100 ms |
25080 KB |
Output is correct |
15 |
Correct |
133 ms |
24952 KB |
Output is correct |
16 |
Correct |
106 ms |
24940 KB |
Output is correct |
17 |
Correct |
137 ms |
24952 KB |
Output is correct |
18 |
Correct |
117 ms |
25080 KB |
Output is correct |
19 |
Correct |
126 ms |
24952 KB |
Output is correct |
20 |
Correct |
107 ms |
24912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
19428 KB |
Output is correct |
2 |
Correct |
86 ms |
24824 KB |
Output is correct |
3 |
Correct |
293 ms |
22824 KB |
Output is correct |
4 |
Correct |
134 ms |
19704 KB |
Output is correct |
5 |
Correct |
198 ms |
22580 KB |
Output is correct |
6 |
Correct |
109 ms |
19700 KB |
Output is correct |
7 |
Correct |
234 ms |
22392 KB |
Output is correct |
8 |
Correct |
188 ms |
19796 KB |
Output is correct |
9 |
Correct |
108 ms |
24972 KB |
Output is correct |
10 |
Correct |
197 ms |
21772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
5 ms |
4984 KB |
Output is correct |
4 |
Correct |
6 ms |
5240 KB |
Output is correct |
5 |
Correct |
126 ms |
17692 KB |
Output is correct |
6 |
Correct |
48 ms |
22648 KB |
Output is correct |
7 |
Correct |
104 ms |
21084 KB |
Output is correct |
8 |
Correct |
87 ms |
18172 KB |
Output is correct |
9 |
Correct |
106 ms |
19960 KB |
Output is correct |
10 |
Correct |
94 ms |
18184 KB |
Output is correct |
11 |
Correct |
6 ms |
5240 KB |
Output is correct |
12 |
Correct |
6 ms |
5240 KB |
Output is correct |
13 |
Correct |
7 ms |
5240 KB |
Output is correct |
14 |
Correct |
6 ms |
5240 KB |
Output is correct |
15 |
Correct |
6 ms |
5240 KB |
Output is correct |
16 |
Correct |
6 ms |
5244 KB |
Output is correct |
17 |
Correct |
6 ms |
5240 KB |
Output is correct |
18 |
Correct |
6 ms |
5368 KB |
Output is correct |
19 |
Correct |
6 ms |
5240 KB |
Output is correct |
20 |
Correct |
6 ms |
5240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
5 ms |
4984 KB |
Output is correct |
4 |
Correct |
6 ms |
5240 KB |
Output is correct |
5 |
Correct |
126 ms |
17692 KB |
Output is correct |
6 |
Correct |
48 ms |
22648 KB |
Output is correct |
7 |
Correct |
104 ms |
21084 KB |
Output is correct |
8 |
Correct |
87 ms |
18172 KB |
Output is correct |
9 |
Correct |
106 ms |
19960 KB |
Output is correct |
10 |
Correct |
94 ms |
18184 KB |
Output is correct |
11 |
Correct |
5 ms |
4984 KB |
Output is correct |
12 |
Correct |
5 ms |
4984 KB |
Output is correct |
13 |
Correct |
6 ms |
5240 KB |
Output is correct |
14 |
Correct |
98 ms |
25080 KB |
Output is correct |
15 |
Correct |
106 ms |
25060 KB |
Output is correct |
16 |
Correct |
78 ms |
24960 KB |
Output is correct |
17 |
Correct |
93 ms |
24952 KB |
Output is correct |
18 |
Correct |
89 ms |
25080 KB |
Output is correct |
19 |
Correct |
86 ms |
25080 KB |
Output is correct |
20 |
Correct |
87 ms |
24952 KB |
Output is correct |
21 |
Correct |
12 ms |
5880 KB |
Output is correct |
22 |
Correct |
98 ms |
24924 KB |
Output is correct |
23 |
Correct |
106 ms |
24980 KB |
Output is correct |
24 |
Correct |
100 ms |
25080 KB |
Output is correct |
25 |
Correct |
133 ms |
24952 KB |
Output is correct |
26 |
Correct |
106 ms |
24940 KB |
Output is correct |
27 |
Correct |
137 ms |
24952 KB |
Output is correct |
28 |
Correct |
117 ms |
25080 KB |
Output is correct |
29 |
Correct |
126 ms |
24952 KB |
Output is correct |
30 |
Correct |
107 ms |
24912 KB |
Output is correct |
31 |
Correct |
146 ms |
19428 KB |
Output is correct |
32 |
Correct |
86 ms |
24824 KB |
Output is correct |
33 |
Correct |
293 ms |
22824 KB |
Output is correct |
34 |
Correct |
134 ms |
19704 KB |
Output is correct |
35 |
Correct |
198 ms |
22580 KB |
Output is correct |
36 |
Correct |
109 ms |
19700 KB |
Output is correct |
37 |
Correct |
234 ms |
22392 KB |
Output is correct |
38 |
Correct |
188 ms |
19796 KB |
Output is correct |
39 |
Correct |
108 ms |
24972 KB |
Output is correct |
40 |
Correct |
197 ms |
21772 KB |
Output is correct |
41 |
Correct |
6 ms |
5240 KB |
Output is correct |
42 |
Correct |
6 ms |
5240 KB |
Output is correct |
43 |
Correct |
7 ms |
5240 KB |
Output is correct |
44 |
Correct |
6 ms |
5240 KB |
Output is correct |
45 |
Correct |
6 ms |
5240 KB |
Output is correct |
46 |
Correct |
6 ms |
5244 KB |
Output is correct |
47 |
Correct |
6 ms |
5240 KB |
Output is correct |
48 |
Correct |
6 ms |
5368 KB |
Output is correct |
49 |
Correct |
6 ms |
5240 KB |
Output is correct |
50 |
Correct |
6 ms |
5240 KB |
Output is correct |
51 |
Correct |
191 ms |
19820 KB |
Output is correct |
52 |
Correct |
120 ms |
24796 KB |
Output is correct |
53 |
Correct |
237 ms |
21744 KB |
Output is correct |
54 |
Correct |
130 ms |
19844 KB |
Output is correct |
55 |
Correct |
158 ms |
19696 KB |
Output is correct |
56 |
Correct |
97 ms |
24824 KB |
Output is correct |
57 |
Correct |
150 ms |
22312 KB |
Output is correct |
58 |
Correct |
95 ms |
19740 KB |
Output is correct |
59 |
Correct |
138 ms |
19784 KB |
Output is correct |
60 |
Correct |
124 ms |
24828 KB |
Output is correct |
61 |
Correct |
245 ms |
22456 KB |
Output is correct |
62 |
Correct |
106 ms |
19760 KB |
Output is correct |
63 |
Correct |
175 ms |
19404 KB |
Output is correct |
64 |
Correct |
132 ms |
24824 KB |
Output is correct |
65 |
Correct |
254 ms |
22392 KB |
Output is correct |
66 |
Correct |
93 ms |
19768 KB |
Output is correct |
67 |
Correct |
129 ms |
19512 KB |
Output is correct |
68 |
Correct |
115 ms |
24924 KB |
Output is correct |
69 |
Correct |
192 ms |
21724 KB |
Output is correct |
70 |
Correct |
130 ms |
19784 KB |
Output is correct |