#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int nmax=100005;
struct qr
{
int n1,n2,cost;
};
vector<qr> L[nmax];
vector<int> v[nmax];
int tt[20][nmax];
int l[nmax],r[nmax],aib[nmax],dp[nmax],sons[nmax];
int n,m,i,j,nr,a,b,c,al,bl,t;
inline int lbit(int x)
{
return ((x^(x-1))&x);
}
void update(int poz,int val)
{
for(int idx=poz;idx<=n;idx+=lbit(idx))
aib[idx]+=val;
}
int compute(int poz)
{
int ret=0;
for(int idx=poz;idx>0;idx-=lbit(idx))
ret+=aib[idx];
return ret;
}
void dfs(int x)
{
l[x]=++nr;
for(int i=0;i<v[x].size();i++)
if(!l[v[x][i]])
{
tt[0][v[x][i]]=x;
dfs(v[x][i]);
}
r[x]=nr;
}
void chef()
{
for(int i=1;i<=16;i++)
for(int j=1;j<=n;j++)
tt[i][j]=tt[i-1][tt[i-1][j]];
}
bool cup(int a,int b)
{
return (l[a]<=l[b]&&l[b]<=r[a]);
}
int lca(int a,int b)
{
int stram=0;
for(int p=16;p>=0;p--)
{
stram=tt[p][a];
if(stram&&(!cup(stram,b)))
a=stram;
}
if(!cup(a,b))
a=tt[0][a];
return a;
}
int nxt(int a,int L)
{
if(a==L) return 0;
for(int p=16;p>=0;p--)
if(tt[p][a]&&(!cup(tt[p][a],L)))
a=tt[p][a];
return a;
}
void defeseu(int x)
{
for(int i=0;i<v[x].size();i++)
if(l[v[x][i]]>l[x])
defeseu(v[x][i]);
int sum=0;
for(int i=0;i<v[x].size();i++)
sum+=dp[v[x][i]];
dp[x]=sum;sons[x]=sum;
for(int i=0;i<L[x].size();i++)
{
a=L[x][i].n1;b=L[x][i].n2;c=L[x][i].cost;
al=nxt(a,x);bl=nxt(b,x);
if(sum-dp[al]-dp[bl]+compute(l[a])+compute(l[b])+c+sons[a]*(a!=x)+sons[b]*(b!=x)>dp[x])
dp[x]=sum-dp[al]-dp[bl]+compute(l[a])+compute(l[b])+c+sons[a]*(a!=x)+sons[b]*(b!=x);
}
int nod;
for(int i=0;i<v[x].size();i++)
if(l[v[x][i]]>l[x])
{
nod=v[x][i];
sum-=dp[nod];
update(l[nod],sum);
update(r[nod]+1,-sum);
sum+=dp[nod];
}
}
int main()
{
// freopen("data.in","r",stdin);
ios_base::sync_with_stdio(false);
cin>>n;
for(i=1;i<=n-1;i++)
{
cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1);
chef();
cin>>m;
for(i=1;i<=m;i++)
{
cin>>a>>b>>c;
t=lca(a,b);
L[t].push_back({a,b,c});
}
defeseu(1);
cout<<dp[1];
return 0;
}
Compilation message
election_campaign.cpp: In function 'void dfs(int)':
election_campaign.cpp:34:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
election_campaign.cpp: In function 'void defeseu(int)':
election_campaign.cpp:75:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
election_campaign.cpp:79:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
election_campaign.cpp:82:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<L[x].size();i++)
~^~~~~~~~~~~~
election_campaign.cpp:90:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5368 KB |
Output is correct |
2 |
Correct |
6 ms |
5480 KB |
Output is correct |
3 |
Correct |
6 ms |
5480 KB |
Output is correct |
4 |
Correct |
7 ms |
5500 KB |
Output is correct |
5 |
Correct |
95 ms |
18500 KB |
Output is correct |
6 |
Correct |
65 ms |
30488 KB |
Output is correct |
7 |
Correct |
98 ms |
30488 KB |
Output is correct |
8 |
Correct |
75 ms |
30488 KB |
Output is correct |
9 |
Correct |
109 ms |
30488 KB |
Output is correct |
10 |
Correct |
70 ms |
30488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30488 KB |
Output is correct |
2 |
Correct |
6 ms |
30488 KB |
Output is correct |
3 |
Correct |
8 ms |
30488 KB |
Output is correct |
4 |
Correct |
243 ms |
39596 KB |
Output is correct |
5 |
Correct |
242 ms |
42160 KB |
Output is correct |
6 |
Correct |
212 ms |
44644 KB |
Output is correct |
7 |
Correct |
228 ms |
47084 KB |
Output is correct |
8 |
Correct |
259 ms |
49772 KB |
Output is correct |
9 |
Correct |
236 ms |
52016 KB |
Output is correct |
10 |
Correct |
227 ms |
54456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30488 KB |
Output is correct |
2 |
Correct |
6 ms |
30488 KB |
Output is correct |
3 |
Correct |
8 ms |
30488 KB |
Output is correct |
4 |
Correct |
243 ms |
39596 KB |
Output is correct |
5 |
Correct |
242 ms |
42160 KB |
Output is correct |
6 |
Correct |
212 ms |
44644 KB |
Output is correct |
7 |
Correct |
228 ms |
47084 KB |
Output is correct |
8 |
Correct |
259 ms |
49772 KB |
Output is correct |
9 |
Correct |
236 ms |
52016 KB |
Output is correct |
10 |
Correct |
227 ms |
54456 KB |
Output is correct |
11 |
Correct |
21 ms |
54456 KB |
Output is correct |
12 |
Correct |
231 ms |
57532 KB |
Output is correct |
13 |
Correct |
230 ms |
60392 KB |
Output is correct |
14 |
Correct |
237 ms |
63124 KB |
Output is correct |
15 |
Correct |
227 ms |
65584 KB |
Output is correct |
16 |
Correct |
206 ms |
68236 KB |
Output is correct |
17 |
Correct |
228 ms |
71092 KB |
Output is correct |
18 |
Correct |
230 ms |
73732 KB |
Output is correct |
19 |
Correct |
211 ms |
76388 KB |
Output is correct |
20 |
Correct |
222 ms |
79112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
200 ms |
79112 KB |
Output is correct |
2 |
Correct |
253 ms |
84100 KB |
Output is correct |
3 |
Correct |
508 ms |
84100 KB |
Output is correct |
4 |
Correct |
159 ms |
84100 KB |
Output is correct |
5 |
Correct |
474 ms |
86532 KB |
Output is correct |
6 |
Correct |
162 ms |
86532 KB |
Output is correct |
7 |
Correct |
460 ms |
91208 KB |
Output is correct |
8 |
Correct |
252 ms |
91208 KB |
Output is correct |
9 |
Correct |
224 ms |
101148 KB |
Output is correct |
10 |
Correct |
381 ms |
101148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5368 KB |
Output is correct |
2 |
Correct |
6 ms |
5480 KB |
Output is correct |
3 |
Correct |
6 ms |
5480 KB |
Output is correct |
4 |
Correct |
7 ms |
5500 KB |
Output is correct |
5 |
Correct |
95 ms |
18500 KB |
Output is correct |
6 |
Correct |
65 ms |
30488 KB |
Output is correct |
7 |
Correct |
98 ms |
30488 KB |
Output is correct |
8 |
Correct |
75 ms |
30488 KB |
Output is correct |
9 |
Correct |
109 ms |
30488 KB |
Output is correct |
10 |
Correct |
70 ms |
30488 KB |
Output is correct |
11 |
Correct |
8 ms |
101148 KB |
Output is correct |
12 |
Correct |
7 ms |
101148 KB |
Output is correct |
13 |
Correct |
8 ms |
101148 KB |
Output is correct |
14 |
Correct |
7 ms |
101148 KB |
Output is correct |
15 |
Correct |
8 ms |
101148 KB |
Output is correct |
16 |
Correct |
7 ms |
101148 KB |
Output is correct |
17 |
Correct |
7 ms |
101148 KB |
Output is correct |
18 |
Correct |
7 ms |
101148 KB |
Output is correct |
19 |
Correct |
7 ms |
101148 KB |
Output is correct |
20 |
Correct |
8 ms |
101148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5368 KB |
Output is correct |
2 |
Correct |
6 ms |
5480 KB |
Output is correct |
3 |
Correct |
6 ms |
5480 KB |
Output is correct |
4 |
Correct |
7 ms |
5500 KB |
Output is correct |
5 |
Correct |
95 ms |
18500 KB |
Output is correct |
6 |
Correct |
65 ms |
30488 KB |
Output is correct |
7 |
Correct |
98 ms |
30488 KB |
Output is correct |
8 |
Correct |
75 ms |
30488 KB |
Output is correct |
9 |
Correct |
109 ms |
30488 KB |
Output is correct |
10 |
Correct |
70 ms |
30488 KB |
Output is correct |
11 |
Correct |
6 ms |
30488 KB |
Output is correct |
12 |
Correct |
6 ms |
30488 KB |
Output is correct |
13 |
Correct |
8 ms |
30488 KB |
Output is correct |
14 |
Correct |
243 ms |
39596 KB |
Output is correct |
15 |
Correct |
242 ms |
42160 KB |
Output is correct |
16 |
Correct |
212 ms |
44644 KB |
Output is correct |
17 |
Correct |
228 ms |
47084 KB |
Output is correct |
18 |
Correct |
259 ms |
49772 KB |
Output is correct |
19 |
Correct |
236 ms |
52016 KB |
Output is correct |
20 |
Correct |
227 ms |
54456 KB |
Output is correct |
21 |
Correct |
21 ms |
54456 KB |
Output is correct |
22 |
Correct |
231 ms |
57532 KB |
Output is correct |
23 |
Correct |
230 ms |
60392 KB |
Output is correct |
24 |
Correct |
237 ms |
63124 KB |
Output is correct |
25 |
Correct |
227 ms |
65584 KB |
Output is correct |
26 |
Correct |
206 ms |
68236 KB |
Output is correct |
27 |
Correct |
228 ms |
71092 KB |
Output is correct |
28 |
Correct |
230 ms |
73732 KB |
Output is correct |
29 |
Correct |
211 ms |
76388 KB |
Output is correct |
30 |
Correct |
222 ms |
79112 KB |
Output is correct |
31 |
Correct |
200 ms |
79112 KB |
Output is correct |
32 |
Correct |
253 ms |
84100 KB |
Output is correct |
33 |
Correct |
508 ms |
84100 KB |
Output is correct |
34 |
Correct |
159 ms |
84100 KB |
Output is correct |
35 |
Correct |
474 ms |
86532 KB |
Output is correct |
36 |
Correct |
162 ms |
86532 KB |
Output is correct |
37 |
Correct |
460 ms |
91208 KB |
Output is correct |
38 |
Correct |
252 ms |
91208 KB |
Output is correct |
39 |
Correct |
224 ms |
101148 KB |
Output is correct |
40 |
Correct |
381 ms |
101148 KB |
Output is correct |
41 |
Correct |
8 ms |
101148 KB |
Output is correct |
42 |
Correct |
7 ms |
101148 KB |
Output is correct |
43 |
Correct |
8 ms |
101148 KB |
Output is correct |
44 |
Correct |
7 ms |
101148 KB |
Output is correct |
45 |
Correct |
8 ms |
101148 KB |
Output is correct |
46 |
Correct |
7 ms |
101148 KB |
Output is correct |
47 |
Correct |
7 ms |
101148 KB |
Output is correct |
48 |
Correct |
7 ms |
101148 KB |
Output is correct |
49 |
Correct |
7 ms |
101148 KB |
Output is correct |
50 |
Correct |
8 ms |
101148 KB |
Output is correct |
51 |
Correct |
260 ms |
101148 KB |
Output is correct |
52 |
Correct |
257 ms |
109484 KB |
Output is correct |
53 |
Correct |
412 ms |
109484 KB |
Output is correct |
54 |
Correct |
169 ms |
109484 KB |
Output is correct |
55 |
Correct |
215 ms |
109484 KB |
Output is correct |
56 |
Correct |
247 ms |
119992 KB |
Output is correct |
57 |
Correct |
488 ms |
119992 KB |
Output is correct |
58 |
Correct |
154 ms |
119992 KB |
Output is correct |
59 |
Correct |
274 ms |
119992 KB |
Output is correct |
60 |
Correct |
226 ms |
131028 KB |
Output is correct |
61 |
Correct |
501 ms |
131028 KB |
Output is correct |
62 |
Correct |
160 ms |
131028 KB |
Output is correct |
63 |
Correct |
219 ms |
131028 KB |
Output is correct |
64 |
Correct |
234 ms |
141664 KB |
Output is correct |
65 |
Correct |
458 ms |
141664 KB |
Output is correct |
66 |
Correct |
167 ms |
141664 KB |
Output is correct |
67 |
Correct |
194 ms |
141664 KB |
Output is correct |
68 |
Correct |
254 ms |
151728 KB |
Output is correct |
69 |
Correct |
498 ms |
151728 KB |
Output is correct |
70 |
Correct |
150 ms |
151728 KB |
Output is correct |