This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define DIM 100010
using namespace std;
vector <int> L[DIM];
pair <int,int> rmq[25][DIM*3],poz[DIM];
int E[DIM*3],p[DIM*3],level[DIM],first[DIM],aib[DIM],sp[DIM],dp[DIM];
struct idk{
int x,y,cost;
};
vector <idk> v[DIM];
int n,m,i,j,x,y,cost,k,idx;
void dfs (int nod, int tata){
level[nod] = 1 + level[tata];
E[++k] = nod;
first[nod] = k;
poz[nod].first = ++idx;
for (auto vecin : L[nod])
if (vecin != tata){
dfs (vecin,nod);
E[++k] = nod;
}
poz[nod].second = idx;
}
int get_lca (int x, int y){
int posx = first[x], posy = first[y];
if (posx > posy)
swap (posx,posy);
int nr = p[posy-posx+1];
pair <int,int> sol = min (rmq[nr][posx],rmq[nr][posy-(1<<nr)+1]);
return E[sol.second];
}
void update (int p, int val){
for (;p<=n;p+=(p&-p))
aib[p] += val;
}
int query (int p){
int sol = 0;
for (;p;p-=(p&-p))
sol += aib[p];
return sol;
}
void dfs2 (int nod, int tata){
for (auto vecin : L[nod])
if (vecin != tata){
dfs2 (vecin,nod);
sp[nod] += dp[vecin];
}
dp[nod] = sp[nod]; /// inca nu iau un lant care are lca in nod
for (auto it : v[nod]){
int x = it.x, y = it.y, cost = it.cost;
dp[nod] = max (dp[nod],query(poz[x].first) + query(poz[y].first) + cost + sp[nod]);
}
update (poz[nod].first, sp[nod] - dp[nod]);
update (poz[nod].second+1, dp[nod] - sp[nod]);
}
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>n;
for (i=1;i<n;i++){
cin>>x>>y;
L[x].push_back(y);
L[y].push_back(x);
}
dfs (1,0);
for (i=1;i<=k;i++)
rmq[0][i] = make_pair(level[E[i]],i);
for (i=1;(1<<i)<=k;i++)
for (j=1;j<=k;j++){
rmq[i][j] = rmq[i-1][j];
if (j + (1<<(i-1)) <= k && rmq[i-1][j + (1<<(i-1))].first < rmq[i][j].first)
rmq[i][j] = rmq[i-1][j + (1<<(i-1))];
}
for (i=2;i<=k;i++)
p[i] = p[i/2] + 1;
cin>>m;
for (i=1;i<=m;i++){
cin>>x>>y>>cost;
v[get_lca(x,y)].push_back({x,y,cost});
}
dfs2 (1,0);
cout<<dp[1];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |