#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#define int long long
using namespace std;
const int maxn = 100000;
const int maxh = 20;
const int root = 1;
int n,m;
vector<int> adj[maxn+1];
vector<int> pathWithTop[maxn+1];
int parentOf[maxn+1][maxh+1];
int directParent[maxn+1];
int a[maxn+1],b[maxn+1],c[maxn+1];
int cnt;
int num[maxn+1],fin[maxn+1],vertex[maxn+1];
int nChain;
int chainHead[maxn+1];
int chainInd[maxn+1];
int posInBase[maxn+1];
int nBase;
int nChild[maxn+1];
int sumOfDP[maxn+1],sumOfChildrenDP[maxn+1]; // store the sum of dp
int dp[maxn+1],dpChildren[maxn+1];
/**
* Code for HLD is adapted from http://vnoi.info/wiki/algo/data-structures/heavy-light-decomposition
* Code for BIT is adapted from http://ntucoder.net/Blog/Details/17
*/
void hld(int u)
{
if (chainHead[nChain]==0)
chainHead[nChain] = u;
chainInd[u] = nChain;
nBase++;
posInBase[u] = nBase;
int mxVtx = -1;
for (int v : adj[u])
if (v!=directParent[u])
{
if (mxVtx == -1 or nChild[v] > nChild[mxVtx])
mxVtx = v;
}
if (mxVtx != -1)
hld(mxVtx);
for (int v : adj[u])
{
if (v!=directParent[u] and v!=mxVtx)
{
nChain++;
hld(v);
}
}
}
void updateBIT(int *bit, int x,int y)
{
for (; x<=n; x+=x&(-x))
bit[x]+=y;
}
void update(bool isSumOfChildrenDP, int u, int value)
{
if (isSumOfChildrenDP)
updateBIT(sumOfChildrenDP,posInBase[u],value);
else
updateBIT(sumOfDP,posInBase[u],value);
}
int getBIT(int *bit,int x)
{
int res = 0;
for (; x>0; x-=x&(-x))
res+=bit[x];
return res;
}
int get(bool isSumOfChildrenDP,int l,int r)
{
if (isSumOfChildrenDP)
return getBIT(sumOfChildrenDP,r) - getBIT(sumOfChildrenDP,l-1);
else
return getBIT(sumOfDP,r) - getBIT(sumOfDP,l-1);
}
/**
* Precondition: a is an ancestor of u
*/
int getHLD(bool isSumOfChildrenDP,int u,int a)
{
int uchain = chainInd[u], achain = chainInd[a];
int res = 0;
while (1) {
if (uchain == achain) {
res+=get(isSumOfChildrenDP, posInBase[a], posInBase[u]);
return res;
}
res+=get(isSumOfChildrenDP, posInBase[chainHead[uchain]], posInBase[u]);
u = directParent[chainHead[uchain]];
uchain = chainInd[u];
}
}
void dfs(int x)
{
cnt++;
num[x] = cnt;
vertex[cnt] = x;
nChild[x] = 1;
for (int y : adj[x])
{
if (num[y]==0)
{
directParent[y] = x;
dfs(y);
nChild[x]+=nChild[y];
}
}
fin[x] = cnt;
}
bool isParent(int x,int y)
{
return (num[x]<=num[y] and fin[y]<=fin[x]);
}
int calcParentOf(int x,int y)
{
if (parentOf[x][y]!=-1)
return parentOf[x][y];
if (x==root)
return parentOf[x][y] = root;
if (y==0)
return parentOf[x][y] = directParent[x];
return parentOf[x][y] = calcParentOf(calcParentOf(x,y-1),y-1);
}
int lca(int x,int y)
{
if (isParent(x,y)) return x;
if (isParent(y,x)) return y;
for (int i=maxh; i>=0; i--)
{
int tmp = calcParentOf(x,i);
if (!isParent(tmp,y))
x = tmp;
}
return directParent[x];
}
/**
* Precondition: x is a proper ancestor of y
*/
int sideParent(int x,int y)
{
for (int u : adj[x])
{
if (isParent(u,y))
return u;
}
return -1;
}
main()
{
ios_base::sync_with_stdio(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
cin >> n;
for (int i=1; i<=n-1; i++)
{
int x,y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(root);
cin >> m;
for (int i=1; i<=m; i++)
cin >> a[i] >> b[i] >> c[i];
memset(parentOf,-1,sizeof(parentOf));
for (int i=1; i<=m; i++)
pathWithTop[lca(a[i],b[i])].push_back(i);
hld(root);
for (int i=n; i>=1; i--)
{
int u = vertex[i];
dp[u] = dpChildren[u]; // choose 0 path
for (int p : pathWithTop[u])
{
int parentOfASide = sideParent(u,a[p]);
int parentOfBSide = sideParent(u,b[p]);
dp[u] = max(dp[u], getHLD(true,a[p],u)+getHLD(true,b[p],u)-dpChildren[u]-getHLD(false,a[p],parentOfASide)-getHLD(false,b[p],parentOfBSide) + c[p]);
}
update(false,u,dp[u]);
if (u!=root)
{
update(true,directParent[u],dp[u]);
dpChildren[directParent[u]]+=dp[u];
}
}
cout << dp[1];
return 0;
}
Compilation message
election_campaign.cpp:184:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main()
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
21752 KB |
Output is correct |
2 |
Correct |
20 ms |
21496 KB |
Output is correct |
3 |
Correct |
20 ms |
21596 KB |
Output is correct |
4 |
Correct |
21 ms |
21752 KB |
Output is correct |
5 |
Correct |
127 ms |
35760 KB |
Output is correct |
6 |
Correct |
74 ms |
39084 KB |
Output is correct |
7 |
Correct |
123 ms |
37980 KB |
Output is correct |
8 |
Correct |
94 ms |
34664 KB |
Output is correct |
9 |
Correct |
113 ms |
37112 KB |
Output is correct |
10 |
Correct |
92 ms |
34680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
21624 KB |
Output is correct |
2 |
Correct |
20 ms |
21496 KB |
Output is correct |
3 |
Correct |
21 ms |
21752 KB |
Output is correct |
4 |
Correct |
141 ms |
44320 KB |
Output is correct |
5 |
Correct |
143 ms |
44408 KB |
Output is correct |
6 |
Correct |
143 ms |
44380 KB |
Output is correct |
7 |
Correct |
144 ms |
44276 KB |
Output is correct |
8 |
Correct |
142 ms |
44252 KB |
Output is correct |
9 |
Correct |
137 ms |
44304 KB |
Output is correct |
10 |
Correct |
141 ms |
44304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
21624 KB |
Output is correct |
2 |
Correct |
20 ms |
21496 KB |
Output is correct |
3 |
Correct |
21 ms |
21752 KB |
Output is correct |
4 |
Correct |
141 ms |
44320 KB |
Output is correct |
5 |
Correct |
143 ms |
44408 KB |
Output is correct |
6 |
Correct |
143 ms |
44380 KB |
Output is correct |
7 |
Correct |
144 ms |
44276 KB |
Output is correct |
8 |
Correct |
142 ms |
44252 KB |
Output is correct |
9 |
Correct |
137 ms |
44304 KB |
Output is correct |
10 |
Correct |
141 ms |
44304 KB |
Output is correct |
11 |
Correct |
30 ms |
22904 KB |
Output is correct |
12 |
Correct |
145 ms |
44604 KB |
Output is correct |
13 |
Correct |
144 ms |
44536 KB |
Output is correct |
14 |
Correct |
140 ms |
44648 KB |
Output is correct |
15 |
Correct |
142 ms |
44536 KB |
Output is correct |
16 |
Correct |
141 ms |
44624 KB |
Output is correct |
17 |
Correct |
144 ms |
44496 KB |
Output is correct |
18 |
Correct |
142 ms |
44528 KB |
Output is correct |
19 |
Correct |
138 ms |
44636 KB |
Output is correct |
20 |
Correct |
143 ms |
44536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
326 ms |
40272 KB |
Output is correct |
2 |
Correct |
133 ms |
44380 KB |
Output is correct |
3 |
Correct |
349 ms |
43128 KB |
Output is correct |
4 |
Correct |
653 ms |
39380 KB |
Output is correct |
5 |
Correct |
265 ms |
42824 KB |
Output is correct |
6 |
Correct |
440 ms |
39472 KB |
Output is correct |
7 |
Correct |
338 ms |
42572 KB |
Output is correct |
8 |
Correct |
258 ms |
40600 KB |
Output is correct |
9 |
Correct |
141 ms |
44536 KB |
Output is correct |
10 |
Correct |
315 ms |
41936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
21752 KB |
Output is correct |
2 |
Correct |
20 ms |
21496 KB |
Output is correct |
3 |
Correct |
20 ms |
21596 KB |
Output is correct |
4 |
Correct |
21 ms |
21752 KB |
Output is correct |
5 |
Correct |
127 ms |
35760 KB |
Output is correct |
6 |
Correct |
74 ms |
39084 KB |
Output is correct |
7 |
Correct |
123 ms |
37980 KB |
Output is correct |
8 |
Correct |
94 ms |
34664 KB |
Output is correct |
9 |
Correct |
113 ms |
37112 KB |
Output is correct |
10 |
Correct |
92 ms |
34680 KB |
Output is correct |
11 |
Correct |
21 ms |
21752 KB |
Output is correct |
12 |
Correct |
21 ms |
21752 KB |
Output is correct |
13 |
Correct |
20 ms |
21752 KB |
Output is correct |
14 |
Correct |
21 ms |
21752 KB |
Output is correct |
15 |
Correct |
22 ms |
21880 KB |
Output is correct |
16 |
Correct |
21 ms |
21684 KB |
Output is correct |
17 |
Correct |
21 ms |
21752 KB |
Output is correct |
18 |
Correct |
21 ms |
21752 KB |
Output is correct |
19 |
Correct |
21 ms |
21752 KB |
Output is correct |
20 |
Correct |
21 ms |
21752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
21752 KB |
Output is correct |
2 |
Correct |
20 ms |
21496 KB |
Output is correct |
3 |
Correct |
20 ms |
21596 KB |
Output is correct |
4 |
Correct |
21 ms |
21752 KB |
Output is correct |
5 |
Correct |
127 ms |
35760 KB |
Output is correct |
6 |
Correct |
74 ms |
39084 KB |
Output is correct |
7 |
Correct |
123 ms |
37980 KB |
Output is correct |
8 |
Correct |
94 ms |
34664 KB |
Output is correct |
9 |
Correct |
113 ms |
37112 KB |
Output is correct |
10 |
Correct |
92 ms |
34680 KB |
Output is correct |
11 |
Correct |
20 ms |
21624 KB |
Output is correct |
12 |
Correct |
20 ms |
21496 KB |
Output is correct |
13 |
Correct |
21 ms |
21752 KB |
Output is correct |
14 |
Correct |
141 ms |
44320 KB |
Output is correct |
15 |
Correct |
143 ms |
44408 KB |
Output is correct |
16 |
Correct |
143 ms |
44380 KB |
Output is correct |
17 |
Correct |
144 ms |
44276 KB |
Output is correct |
18 |
Correct |
142 ms |
44252 KB |
Output is correct |
19 |
Correct |
137 ms |
44304 KB |
Output is correct |
20 |
Correct |
141 ms |
44304 KB |
Output is correct |
21 |
Correct |
30 ms |
22904 KB |
Output is correct |
22 |
Correct |
145 ms |
44604 KB |
Output is correct |
23 |
Correct |
144 ms |
44536 KB |
Output is correct |
24 |
Correct |
140 ms |
44648 KB |
Output is correct |
25 |
Correct |
142 ms |
44536 KB |
Output is correct |
26 |
Correct |
141 ms |
44624 KB |
Output is correct |
27 |
Correct |
144 ms |
44496 KB |
Output is correct |
28 |
Correct |
142 ms |
44528 KB |
Output is correct |
29 |
Correct |
138 ms |
44636 KB |
Output is correct |
30 |
Correct |
143 ms |
44536 KB |
Output is correct |
31 |
Correct |
326 ms |
40272 KB |
Output is correct |
32 |
Correct |
133 ms |
44380 KB |
Output is correct |
33 |
Correct |
349 ms |
43128 KB |
Output is correct |
34 |
Correct |
653 ms |
39380 KB |
Output is correct |
35 |
Correct |
265 ms |
42824 KB |
Output is correct |
36 |
Correct |
440 ms |
39472 KB |
Output is correct |
37 |
Correct |
338 ms |
42572 KB |
Output is correct |
38 |
Correct |
258 ms |
40600 KB |
Output is correct |
39 |
Correct |
141 ms |
44536 KB |
Output is correct |
40 |
Correct |
315 ms |
41936 KB |
Output is correct |
41 |
Correct |
21 ms |
21752 KB |
Output is correct |
42 |
Correct |
21 ms |
21752 KB |
Output is correct |
43 |
Correct |
20 ms |
21752 KB |
Output is correct |
44 |
Correct |
21 ms |
21752 KB |
Output is correct |
45 |
Correct |
22 ms |
21880 KB |
Output is correct |
46 |
Correct |
21 ms |
21684 KB |
Output is correct |
47 |
Correct |
21 ms |
21752 KB |
Output is correct |
48 |
Correct |
21 ms |
21752 KB |
Output is correct |
49 |
Correct |
21 ms |
21752 KB |
Output is correct |
50 |
Correct |
21 ms |
21752 KB |
Output is correct |
51 |
Correct |
259 ms |
40852 KB |
Output is correct |
52 |
Correct |
144 ms |
44508 KB |
Output is correct |
53 |
Correct |
325 ms |
42340 KB |
Output is correct |
54 |
Correct |
446 ms |
39540 KB |
Output is correct |
55 |
Correct |
322 ms |
40516 KB |
Output is correct |
56 |
Correct |
147 ms |
44636 KB |
Output is correct |
57 |
Correct |
275 ms |
42864 KB |
Output is correct |
58 |
Correct |
709 ms |
39540 KB |
Output is correct |
59 |
Correct |
263 ms |
40836 KB |
Output is correct |
60 |
Correct |
144 ms |
44536 KB |
Output is correct |
61 |
Correct |
293 ms |
42804 KB |
Output is correct |
62 |
Correct |
679 ms |
39544 KB |
Output is correct |
63 |
Correct |
316 ms |
40480 KB |
Output is correct |
64 |
Correct |
140 ms |
44536 KB |
Output is correct |
65 |
Correct |
317 ms |
42736 KB |
Output is correct |
66 |
Correct |
552 ms |
39924 KB |
Output is correct |
67 |
Correct |
319 ms |
40428 KB |
Output is correct |
68 |
Correct |
143 ms |
44664 KB |
Output is correct |
69 |
Correct |
301 ms |
42012 KB |
Output is correct |
70 |
Correct |
568 ms |
39648 KB |
Output is correct |