#include <bits/stdc++.h>
using namespace std;
int nr[200005],niv[200005],tatic[200005],q,q2,prim[200005],mar[200005],max_fiu[200005],cnt[200005],primul[200005],id[200005],poz[200005];
vector <int> v[200005];
int rmq[200005][20];
void dfs(int x,int tata,int nivel)
{
nr[++q2]=x;
niv[x]=nivel;
mar[x]=1;
tatic[x]=tata;
if (prim[x]==0)
{
prim[x]=q2;
}
for (int i=0;i<v[x].size();i++)
{
int nod=v[x][i];
if (tata!=nod)
{
dfs(nod,x,nivel+1);
mar[x]+=mar[nod];
if (mar[nod]>mar[max_fiu[x]])
{
max_fiu[x]=nod;
}
nr[++q2]=x;
}
}
}
void split (int x,int t)
{
cnt[q]++;
if (cnt[q]==1)
{
primul[q]=x;
}
id[x]=q;
poz[x]=cnt[q];
if (mar[x]==1)
{
return;
}
split(max_fiu[x],x);
for (int i=0;i<v[x].size();i++)
{
int nod=v[x][i];
if (nod!=t&&nod!=max_fiu[x])
{
q++;
split(nod,x);
}
}
}
int lca(int x,int y)
{
if (x==y)
{
return x;
}
x=prim[x];
y=prim[y];
if (x>y)
{
swap(x,y);
}
int lg=log2(y-x);
if (niv[rmq[x][lg]]<niv[rmq[y-(1<<lg)+1][lg]])
{
return rmq[x][lg];
}
else
{
return rmq[y-(1<<lg)+1][lg];
}
}
struct wow
{
int x,y,z;
};
vector <wow> sal[200005];
int din[200005],sumafii[200005];
class SegmentTree
{
int *arb;
public:
inline void Initialize(int Elems)
{
arb = new int [((Elems<<4)+1)];
for (int i=1;i<=(Elems<<2);i++)
{
arb[i]=0;
}
return;
}
inline void update(int st,int dr,int nod,int poz,int val)
{
if (st==dr)
{
arb[nod]=val;
return;
}
int mij=(st+dr)/2;
if (poz<=mij)
{
update(st,mij,2*nod,poz,val);
}
else
{
update(mij+1,dr,2*nod+1,poz,val);
}
arb[nod]=arb[2*nod+1]+arb[2*nod];
}
inline int query(int st,int dr,int nod,int ua,int ub)
{
if (ua<=st&&dr<=ub)
{
return arb[nod];
}
int mij=(st+dr)/2;
int sum=0;
if (ua<=mij)
{
sum=sum+query(st,mij,2*nod,ua,ub);
}
if (mij<ub)
{
sum=sum+query(mij+1,dr,2*nod+1,ua,ub);
}
return sum;
}
}*chains;
int query(int x,int y)
{
if (id[x]==id[y])
{
int st,dr;
st=poz[x];
dr=poz[y];
if (st>dr)
{
swap(st,dr);
}
return chains[id[x]].query(1,cnt[id[x]],1,st,dr);
}
if (niv[primul[id[x]]]>niv[primul[id[y]]])
{
swap(x,y);
}
return query(y,primul[id[y]])+query(tatic[primul[id[y]]],x);
}
void solve(int x,int tata)
{
int i;
for (i=0;i<v[x].size();i++)
{
if (v[x][i]!=tata)
{
solve(v[x][i],x);
}
}
for (i=0;i<v[x].size();i++)
{
if (v[x][i]!=tata)
{
sumafii[x]+=din[v[x][i]];
}
}
din[x]=sumafii[x];
for (i=0;i<sal[x].size();i++)
{
int suma=sumafii[x]+query(x,sal[x][i].x)+query(x,sal[x][i].y);
din[x]=max(din[x],suma+sal[x][i].z);
}
chains[id[x]].update(1,cnt[id[x]],1,poz[x],sumafii[x]-din[x]);
}
int n,i,x,y,lg,p,j,m,z;
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(0);
#ifdef HOME
ifstream cin("date.in");
ofstream cout("date.out");
#endif // HOME
cin>>n;
for (i=1;i<n;i++)
{
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1,0,1);
q=1;
split(1,0);
chains = new SegmentTree[(q+1)];
for (i=1;i<=q;i++)
{
chains[i].Initialize(cnt[i]);
}
for (i=1;i<=q2;i++)
{
rmq[i][0]=nr[i];
}
lg=log2(q2);
p=1;
for (i=1;i<=lg;i++)
{
for (j=1;j<=q2-p+1;j++)
{
if (niv[rmq[j][i-1]]<niv[rmq[j+p][i-1]])
{
rmq[j][i]=rmq[j][i-1];
}
else
{
rmq[j][i]=rmq[j+p][i-1];
}
}
p=p*2;
}
cin>>m;
for (i=1;i<=m;i++)
{
cin>>x>>y>>z;
sal[lca(x,y)].push_back(wow{x,y,z});
}
solve(1,0);
cout<<din[1];
return 0;
}
Compilation message
election_campaign.cpp: In function 'void dfs(int, int, int)':
election_campaign.cpp:17:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for (int i=0;i<v[x].size();i++)
| ~^~~~~~~~~~~~
election_campaign.cpp: In function 'void split(int, int)':
election_campaign.cpp:46:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (int i=0;i<v[x].size();i++)
| ~^~~~~~~~~~~~
election_campaign.cpp: In function 'void solve(int, int)':
election_campaign.cpp:157:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
157 | for (i=0;i<v[x].size();i++)
| ~^~~~~~~~~~~~
election_campaign.cpp:164:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
164 | for (i=0;i<v[x].size();i++)
| ~^~~~~~~~~~~~
election_campaign.cpp:172:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<wow>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
172 | for (i=0;i<sal[x].size();i++)
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9724 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
10060 KB |
Output is correct |
5 |
Correct |
126 ms |
40852 KB |
Output is correct |
6 |
Correct |
90 ms |
42232 KB |
Output is correct |
7 |
Correct |
122 ms |
41896 KB |
Output is correct |
8 |
Correct |
98 ms |
42044 KB |
Output is correct |
9 |
Correct |
113 ms |
40332 KB |
Output is correct |
10 |
Correct |
102 ms |
42052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
10060 KB |
Output is correct |
4 |
Correct |
181 ms |
44212 KB |
Output is correct |
5 |
Correct |
188 ms |
46672 KB |
Output is correct |
6 |
Correct |
177 ms |
46660 KB |
Output is correct |
7 |
Correct |
187 ms |
46640 KB |
Output is correct |
8 |
Correct |
193 ms |
46696 KB |
Output is correct |
9 |
Correct |
177 ms |
46732 KB |
Output is correct |
10 |
Correct |
184 ms |
46652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
10060 KB |
Output is correct |
4 |
Correct |
181 ms |
44212 KB |
Output is correct |
5 |
Correct |
188 ms |
46672 KB |
Output is correct |
6 |
Correct |
177 ms |
46660 KB |
Output is correct |
7 |
Correct |
187 ms |
46640 KB |
Output is correct |
8 |
Correct |
193 ms |
46696 KB |
Output is correct |
9 |
Correct |
177 ms |
46732 KB |
Output is correct |
10 |
Correct |
184 ms |
46652 KB |
Output is correct |
11 |
Correct |
21 ms |
10896 KB |
Output is correct |
12 |
Correct |
191 ms |
47084 KB |
Output is correct |
13 |
Correct |
197 ms |
47120 KB |
Output is correct |
14 |
Correct |
191 ms |
46944 KB |
Output is correct |
15 |
Correct |
191 ms |
46984 KB |
Output is correct |
16 |
Correct |
171 ms |
46896 KB |
Output is correct |
17 |
Correct |
199 ms |
46984 KB |
Output is correct |
18 |
Correct |
192 ms |
47060 KB |
Output is correct |
19 |
Correct |
177 ms |
47044 KB |
Output is correct |
20 |
Correct |
194 ms |
46916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
268 ms |
42268 KB |
Output is correct |
2 |
Correct |
177 ms |
44156 KB |
Output is correct |
3 |
Correct |
226 ms |
46020 KB |
Output is correct |
4 |
Correct |
208 ms |
46100 KB |
Output is correct |
5 |
Correct |
215 ms |
45620 KB |
Output is correct |
6 |
Correct |
166 ms |
45980 KB |
Output is correct |
7 |
Correct |
261 ms |
45184 KB |
Output is correct |
8 |
Correct |
257 ms |
44932 KB |
Output is correct |
9 |
Correct |
179 ms |
46768 KB |
Output is correct |
10 |
Correct |
230 ms |
44208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9724 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
10060 KB |
Output is correct |
5 |
Correct |
126 ms |
40852 KB |
Output is correct |
6 |
Correct |
90 ms |
42232 KB |
Output is correct |
7 |
Correct |
122 ms |
41896 KB |
Output is correct |
8 |
Correct |
98 ms |
42044 KB |
Output is correct |
9 |
Correct |
113 ms |
40332 KB |
Output is correct |
10 |
Correct |
102 ms |
42052 KB |
Output is correct |
11 |
Correct |
6 ms |
10060 KB |
Output is correct |
12 |
Correct |
7 ms |
10060 KB |
Output is correct |
13 |
Correct |
6 ms |
10080 KB |
Output is correct |
14 |
Correct |
6 ms |
10060 KB |
Output is correct |
15 |
Correct |
7 ms |
10108 KB |
Output is correct |
16 |
Correct |
8 ms |
10060 KB |
Output is correct |
17 |
Correct |
7 ms |
10068 KB |
Output is correct |
18 |
Correct |
7 ms |
10060 KB |
Output is correct |
19 |
Correct |
7 ms |
10060 KB |
Output is correct |
20 |
Correct |
7 ms |
10132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9724 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
10060 KB |
Output is correct |
5 |
Correct |
126 ms |
40852 KB |
Output is correct |
6 |
Correct |
90 ms |
42232 KB |
Output is correct |
7 |
Correct |
122 ms |
41896 KB |
Output is correct |
8 |
Correct |
98 ms |
42044 KB |
Output is correct |
9 |
Correct |
113 ms |
40332 KB |
Output is correct |
10 |
Correct |
102 ms |
42052 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
7 ms |
10060 KB |
Output is correct |
14 |
Correct |
181 ms |
44212 KB |
Output is correct |
15 |
Correct |
188 ms |
46672 KB |
Output is correct |
16 |
Correct |
177 ms |
46660 KB |
Output is correct |
17 |
Correct |
187 ms |
46640 KB |
Output is correct |
18 |
Correct |
193 ms |
46696 KB |
Output is correct |
19 |
Correct |
177 ms |
46732 KB |
Output is correct |
20 |
Correct |
184 ms |
46652 KB |
Output is correct |
21 |
Correct |
21 ms |
10896 KB |
Output is correct |
22 |
Correct |
191 ms |
47084 KB |
Output is correct |
23 |
Correct |
197 ms |
47120 KB |
Output is correct |
24 |
Correct |
191 ms |
46944 KB |
Output is correct |
25 |
Correct |
191 ms |
46984 KB |
Output is correct |
26 |
Correct |
171 ms |
46896 KB |
Output is correct |
27 |
Correct |
199 ms |
46984 KB |
Output is correct |
28 |
Correct |
192 ms |
47060 KB |
Output is correct |
29 |
Correct |
177 ms |
47044 KB |
Output is correct |
30 |
Correct |
194 ms |
46916 KB |
Output is correct |
31 |
Correct |
268 ms |
42268 KB |
Output is correct |
32 |
Correct |
177 ms |
44156 KB |
Output is correct |
33 |
Correct |
226 ms |
46020 KB |
Output is correct |
34 |
Correct |
208 ms |
46100 KB |
Output is correct |
35 |
Correct |
215 ms |
45620 KB |
Output is correct |
36 |
Correct |
166 ms |
45980 KB |
Output is correct |
37 |
Correct |
261 ms |
45184 KB |
Output is correct |
38 |
Correct |
257 ms |
44932 KB |
Output is correct |
39 |
Correct |
179 ms |
46768 KB |
Output is correct |
40 |
Correct |
230 ms |
44208 KB |
Output is correct |
41 |
Correct |
6 ms |
10060 KB |
Output is correct |
42 |
Correct |
7 ms |
10060 KB |
Output is correct |
43 |
Correct |
6 ms |
10080 KB |
Output is correct |
44 |
Correct |
6 ms |
10060 KB |
Output is correct |
45 |
Correct |
7 ms |
10108 KB |
Output is correct |
46 |
Correct |
8 ms |
10060 KB |
Output is correct |
47 |
Correct |
7 ms |
10068 KB |
Output is correct |
48 |
Correct |
7 ms |
10060 KB |
Output is correct |
49 |
Correct |
7 ms |
10060 KB |
Output is correct |
50 |
Correct |
7 ms |
10132 KB |
Output is correct |
51 |
Correct |
217 ms |
45348 KB |
Output is correct |
52 |
Correct |
184 ms |
46984 KB |
Output is correct |
53 |
Correct |
233 ms |
44588 KB |
Output is correct |
54 |
Correct |
166 ms |
46576 KB |
Output is correct |
55 |
Correct |
236 ms |
45028 KB |
Output is correct |
56 |
Correct |
190 ms |
47068 KB |
Output is correct |
57 |
Correct |
240 ms |
45304 KB |
Output is correct |
58 |
Correct |
171 ms |
46204 KB |
Output is correct |
59 |
Correct |
211 ms |
45328 KB |
Output is correct |
60 |
Correct |
183 ms |
46916 KB |
Output is correct |
61 |
Correct |
231 ms |
45376 KB |
Output is correct |
62 |
Correct |
177 ms |
46080 KB |
Output is correct |
63 |
Correct |
237 ms |
44784 KB |
Output is correct |
64 |
Correct |
183 ms |
46880 KB |
Output is correct |
65 |
Correct |
256 ms |
45164 KB |
Output is correct |
66 |
Correct |
157 ms |
46208 KB |
Output is correct |
67 |
Correct |
234 ms |
44916 KB |
Output is correct |
68 |
Correct |
190 ms |
46908 KB |
Output is correct |
69 |
Correct |
207 ms |
44160 KB |
Output is correct |
70 |
Correct |
188 ms |
46368 KB |
Output is correct |