#include<bits/stdc++.h>
#define MAX 100100
#define LOG 17
#define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;i=i+1)
#define FORD(i,b,a) for (int i=(b),_a=(a);i>=_a;i=i-1)
#define REP(i,n) for (int i=0,_n=(n);i<_n;i=i+1)
#define FORE(i,v) for (__typeof((v).begin()) i=(v).begin();i!=(v).end();i++)
using namespace std;
class SegmentTree {
private:
int n;
vector<int> lazy;
void update(int i,int l,int r,int u,int v,int c) {
if (l>v || r<u || l>r || v<u) return;
if (u<=l && r<=v) {
lazy[i]+=c;
return;
}
int m=(l+r)>>1;
update(2*i,l,m,u,v,c);
update(2*i+1,m+1,r,u,v,c);
}
public:
SegmentTree() {
n=0;
}
SegmentTree(int n) {
this->n=n;
lazy.assign(4*n+7,0);
}
void update(int l,int r,int c) {
update(1,1,n,l,r,c);
}
int get(int x) const {
int res=0;
int i=1;
int l=1;
int r=n;
while (true) {
res+=lazy[i];
if (l==r) return (res);
int m=(l+r)>>1;
if (x>m) {
i=2*i+1;
l=m+1;
} else {
i=2*i;
r=m;
}
}
}
};
struct Req {
int u,v,c;
Req() {
u=v=c=0;
}
void input(void) {
scanf("%d%d%d",&u,&v,&c);
}
};
Req req[MAX];
vector<int> adj[MAX];
int n,m,cnt;
int sta[MAX],fin[MAX],high[MAX],par[MAX][LOG+1];
vector<int> reqAt[MAX];
int f[MAX],sumF[MAX];
SegmentTree myit;
void loadTree(void) {
scanf("%d",&n);
REP(love,n-1) {
int u,v;
scanf("%d%d",&u,&v);
adj[u].push_back(v);
adj[v].push_back(u);
}
scanf("%d",&m);
FOR(i,1,m) req[i].input();
}
void dfs(int u) {
sta[u]=++cnt;
FORE(it,adj[u]) if (*it!=par[u][0]) {
int v=*it;
high[v]=high[u]+1;
par[v][0]=u;
dfs(v);
}
fin[u]=cnt;
}
int LCA(int u,int v) {
if (high[u]<high[v]) return (LCA(v,u));
FORD(i,LOG,0) if (high[par[u][i]]>=high[v]) u=par[u][i];
if (u==v) return (u);
FORD(i,LOG,0) if (par[u][i]!=par[v][i]) {
u=par[u][i];
v=par[v][i];
}
return (par[u][0]);
}
int jump(int u,int h) {
FORD(i,LOG,0) if (high[par[u][i]]>=h) u=par[u][i];
return (u);
}
void prepare(void) {
high[0]=-1;
dfs(1);
FOR(j,1,LOG) FOR(i,1,n) par[i][j]=par[par[i][j-1]][j-1];
FOR(i,1,m) reqAt[LCA(req[i].u,req[i].v)].push_back(i);
myit=SegmentTree(n);
}
void dp(int u) {
FORE(it,adj[u]) if (*it!=par[u][0]) {
int v=*it;
dp(v);
sumF[u]+=f[v];
}
f[u]=sumF[u];
/*printf("To calculate %d\n",u);
FOR(i,1,n) if (LCA(i,u)==u) printf("Value of %d is %d\n",i,myit.get(sta[i]));
printf("END\n");*/
FORE(it,reqAt[u]) {
int x=req[*it].u;
int y=req[*it].v;
int px=jump(x,high[u]+1);
int py=jump(y,high[u]+1);
int tmp=sumF[u];
if (x!=u) tmp+=myit.get(sta[x])-f[px];
if (y!=u) tmp+=myit.get(sta[y])-f[py];
//printf("With req %d (%d,%d,%d) at %d have %d\n",*it,req[*it].u,req[*it].v,req[*it].c,u,tmp+req[*it].c);
f[u]=max(f[u],tmp+req[*it].c);
}
FORE(it,adj[u]) if (*it!=par[u][0]) {
int v=*it;
myit.update(sta[v],fin[v],sumF[u]-f[v]);
}
myit.update(sta[u],sta[u],sumF[u]);
//printf("F %d is %d\n",u,f[u]);
}
void process(void) {
dp(1);
printf("%d\n",f[1]);
}
int main(void) {
//freopen("test.in","r",stdin);
loadTree();
prepare();
process();
return 0;
}
Compilation message
election_campaign.cpp: In function 'void loadTree()':
election_campaign.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
election_campaign.cpp:73:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d%d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
election_campaign.cpp:77:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d",&m);
| ~~~~~^~~~~~~~~
election_campaign.cpp: In member function 'void Req::input()':
election_campaign.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d%d%d",&u,&v,&c);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6100 KB |
Output is correct |
2 |
Correct |
5 ms |
6228 KB |
Output is correct |
3 |
Correct |
4 ms |
6100 KB |
Output is correct |
4 |
Correct |
4 ms |
6356 KB |
Output is correct |
5 |
Correct |
96 ms |
21156 KB |
Output is correct |
6 |
Correct |
74 ms |
36656 KB |
Output is correct |
7 |
Correct |
125 ms |
31224 KB |
Output is correct |
8 |
Correct |
86 ms |
21104 KB |
Output is correct |
9 |
Correct |
104 ms |
28100 KB |
Output is correct |
10 |
Correct |
91 ms |
21104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6100 KB |
Output is correct |
2 |
Correct |
5 ms |
6228 KB |
Output is correct |
3 |
Correct |
4 ms |
6484 KB |
Output is correct |
4 |
Correct |
181 ms |
39040 KB |
Output is correct |
5 |
Correct |
173 ms |
39044 KB |
Output is correct |
6 |
Correct |
158 ms |
39024 KB |
Output is correct |
7 |
Correct |
189 ms |
39008 KB |
Output is correct |
8 |
Correct |
160 ms |
39052 KB |
Output is correct |
9 |
Correct |
159 ms |
39052 KB |
Output is correct |
10 |
Correct |
172 ms |
38992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6100 KB |
Output is correct |
2 |
Correct |
5 ms |
6228 KB |
Output is correct |
3 |
Correct |
4 ms |
6484 KB |
Output is correct |
4 |
Correct |
181 ms |
39040 KB |
Output is correct |
5 |
Correct |
173 ms |
39044 KB |
Output is correct |
6 |
Correct |
158 ms |
39024 KB |
Output is correct |
7 |
Correct |
189 ms |
39008 KB |
Output is correct |
8 |
Correct |
160 ms |
39052 KB |
Output is correct |
9 |
Correct |
159 ms |
39052 KB |
Output is correct |
10 |
Correct |
172 ms |
38992 KB |
Output is correct |
11 |
Correct |
18 ms |
6900 KB |
Output is correct |
12 |
Correct |
173 ms |
39356 KB |
Output is correct |
13 |
Correct |
194 ms |
39368 KB |
Output is correct |
14 |
Correct |
155 ms |
39372 KB |
Output is correct |
15 |
Correct |
165 ms |
39360 KB |
Output is correct |
16 |
Correct |
186 ms |
39344 KB |
Output is correct |
17 |
Correct |
158 ms |
39304 KB |
Output is correct |
18 |
Correct |
169 ms |
39404 KB |
Output is correct |
19 |
Correct |
174 ms |
39348 KB |
Output is correct |
20 |
Correct |
170 ms |
39308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
22996 KB |
Output is correct |
2 |
Correct |
168 ms |
39040 KB |
Output is correct |
3 |
Correct |
273 ms |
33084 KB |
Output is correct |
4 |
Correct |
172 ms |
22984 KB |
Output is correct |
5 |
Correct |
241 ms |
32168 KB |
Output is correct |
6 |
Correct |
166 ms |
23044 KB |
Output is correct |
7 |
Correct |
297 ms |
31816 KB |
Output is correct |
8 |
Correct |
194 ms |
23424 KB |
Output is correct |
9 |
Correct |
151 ms |
39100 KB |
Output is correct |
10 |
Correct |
268 ms |
29980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6100 KB |
Output is correct |
2 |
Correct |
5 ms |
6228 KB |
Output is correct |
3 |
Correct |
4 ms |
6100 KB |
Output is correct |
4 |
Correct |
4 ms |
6356 KB |
Output is correct |
5 |
Correct |
96 ms |
21156 KB |
Output is correct |
6 |
Correct |
74 ms |
36656 KB |
Output is correct |
7 |
Correct |
125 ms |
31224 KB |
Output is correct |
8 |
Correct |
86 ms |
21104 KB |
Output is correct |
9 |
Correct |
104 ms |
28100 KB |
Output is correct |
10 |
Correct |
91 ms |
21104 KB |
Output is correct |
11 |
Correct |
5 ms |
6356 KB |
Output is correct |
12 |
Correct |
4 ms |
6484 KB |
Output is correct |
13 |
Correct |
4 ms |
6484 KB |
Output is correct |
14 |
Correct |
4 ms |
6336 KB |
Output is correct |
15 |
Correct |
5 ms |
6372 KB |
Output is correct |
16 |
Correct |
5 ms |
6340 KB |
Output is correct |
17 |
Correct |
7 ms |
6464 KB |
Output is correct |
18 |
Correct |
5 ms |
6464 KB |
Output is correct |
19 |
Correct |
4 ms |
6336 KB |
Output is correct |
20 |
Correct |
5 ms |
6484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6100 KB |
Output is correct |
2 |
Correct |
5 ms |
6228 KB |
Output is correct |
3 |
Correct |
4 ms |
6100 KB |
Output is correct |
4 |
Correct |
4 ms |
6356 KB |
Output is correct |
5 |
Correct |
96 ms |
21156 KB |
Output is correct |
6 |
Correct |
74 ms |
36656 KB |
Output is correct |
7 |
Correct |
125 ms |
31224 KB |
Output is correct |
8 |
Correct |
86 ms |
21104 KB |
Output is correct |
9 |
Correct |
104 ms |
28100 KB |
Output is correct |
10 |
Correct |
91 ms |
21104 KB |
Output is correct |
11 |
Correct |
4 ms |
6100 KB |
Output is correct |
12 |
Correct |
5 ms |
6228 KB |
Output is correct |
13 |
Correct |
4 ms |
6484 KB |
Output is correct |
14 |
Correct |
181 ms |
39040 KB |
Output is correct |
15 |
Correct |
173 ms |
39044 KB |
Output is correct |
16 |
Correct |
158 ms |
39024 KB |
Output is correct |
17 |
Correct |
189 ms |
39008 KB |
Output is correct |
18 |
Correct |
160 ms |
39052 KB |
Output is correct |
19 |
Correct |
159 ms |
39052 KB |
Output is correct |
20 |
Correct |
172 ms |
38992 KB |
Output is correct |
21 |
Correct |
18 ms |
6900 KB |
Output is correct |
22 |
Correct |
173 ms |
39356 KB |
Output is correct |
23 |
Correct |
194 ms |
39368 KB |
Output is correct |
24 |
Correct |
155 ms |
39372 KB |
Output is correct |
25 |
Correct |
165 ms |
39360 KB |
Output is correct |
26 |
Correct |
186 ms |
39344 KB |
Output is correct |
27 |
Correct |
158 ms |
39304 KB |
Output is correct |
28 |
Correct |
169 ms |
39404 KB |
Output is correct |
29 |
Correct |
174 ms |
39348 KB |
Output is correct |
30 |
Correct |
170 ms |
39308 KB |
Output is correct |
31 |
Correct |
196 ms |
22996 KB |
Output is correct |
32 |
Correct |
168 ms |
39040 KB |
Output is correct |
33 |
Correct |
273 ms |
33084 KB |
Output is correct |
34 |
Correct |
172 ms |
22984 KB |
Output is correct |
35 |
Correct |
241 ms |
32168 KB |
Output is correct |
36 |
Correct |
166 ms |
23044 KB |
Output is correct |
37 |
Correct |
297 ms |
31816 KB |
Output is correct |
38 |
Correct |
194 ms |
23424 KB |
Output is correct |
39 |
Correct |
151 ms |
39100 KB |
Output is correct |
40 |
Correct |
268 ms |
29980 KB |
Output is correct |
41 |
Correct |
5 ms |
6356 KB |
Output is correct |
42 |
Correct |
4 ms |
6484 KB |
Output is correct |
43 |
Correct |
4 ms |
6484 KB |
Output is correct |
44 |
Correct |
4 ms |
6336 KB |
Output is correct |
45 |
Correct |
5 ms |
6372 KB |
Output is correct |
46 |
Correct |
5 ms |
6340 KB |
Output is correct |
47 |
Correct |
7 ms |
6464 KB |
Output is correct |
48 |
Correct |
5 ms |
6464 KB |
Output is correct |
49 |
Correct |
4 ms |
6336 KB |
Output is correct |
50 |
Correct |
5 ms |
6484 KB |
Output is correct |
51 |
Correct |
199 ms |
23748 KB |
Output is correct |
52 |
Correct |
167 ms |
39332 KB |
Output is correct |
53 |
Correct |
256 ms |
30456 KB |
Output is correct |
54 |
Correct |
152 ms |
23328 KB |
Output is correct |
55 |
Correct |
193 ms |
23296 KB |
Output is correct |
56 |
Correct |
166 ms |
39288 KB |
Output is correct |
57 |
Correct |
234 ms |
31448 KB |
Output is correct |
58 |
Correct |
160 ms |
23260 KB |
Output is correct |
59 |
Correct |
190 ms |
23628 KB |
Output is correct |
60 |
Correct |
165 ms |
39292 KB |
Output is correct |
61 |
Correct |
230 ms |
31748 KB |
Output is correct |
62 |
Correct |
159 ms |
23180 KB |
Output is correct |
63 |
Correct |
193 ms |
23328 KB |
Output is correct |
64 |
Correct |
168 ms |
39352 KB |
Output is correct |
65 |
Correct |
261 ms |
31592 KB |
Output is correct |
66 |
Correct |
152 ms |
23332 KB |
Output is correct |
67 |
Correct |
191 ms |
23240 KB |
Output is correct |
68 |
Correct |
164 ms |
39404 KB |
Output is correct |
69 |
Correct |
225 ms |
29296 KB |
Output is correct |
70 |
Correct |
162 ms |
23276 KB |
Output is correct |