#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int N=200050;
vector<pair<int,int>> E[N];
ll sum[N],all_up;
int up[N],down[N];
void DFS(int u, int p)
{
for(auto e:E[u])
{
int v=e.first,w=e.second;
if(v==p) up[u]=w,all_up+=w;
}
if(p) sum[u]=sum[p]+down[u]-up[u];
for(auto e:E[u])
{
int v=e.first,w=e.second;
if(v!=p)
{
down[v]=w;
DFS(v,u);
}
}
}
ll dep[N];
int lid[N],rid[N],tid,nod[N],par[N],my[N],rt,n;
void DFS2(int u, int p)
{
lid[u]=++tid;
nod[tid]=u;
par[u]=p;
my[u]=u;
for(auto e:E[u]) if(e.first!=p)
{
int v=e.first;
dep[v]=dep[u]+e.second;
DFS2(v,u);
if(dep[my[u]]<dep[my[v]]) my[u]=my[v];
}
rid[u]=tid;
}
const int M=2*N;
int ls[M],rs[M],tsz,root,id[M];
ll mn[M],lzy[M];
void Build(int &c, int ss, int se)
{
c=++tsz;
if(ss==se){ mn[c]=dep[nod[ss]];id[c]=nod[ss];return;}
int mid=ss+se>>1;
Build(ls[c],ss,mid);
Build(rs[c],mid+1,se);
mn[c]=min(mn[ls[c]],mn[rs[c]]);
if(mn[c]==mn[ls[c]]) id[c]=id[ls[c]];
else id[c]=id[rs[c]];
}
void Add(int c, int ss, int se, int qs, int qe, ll x)
{
if(qs>qe || qs>se || ss>qe) return;
if(qs<=ss && qe>=se){ mn[c]+=x;lzy[c]+=x;return;}
int mid=ss+se>>1;
Add(ls[c],ss,mid,qs,qe,x);
Add(rs[c],mid+1,se,qs,qe,x);
mn[c]=min(mn[ls[c]],mn[rs[c]]);
if(mn[c]==mn[ls[c]]) id[c]=id[ls[c]];
else id[c]=id[rs[c]];
mn[c]+=lzy[c];
}
bool was[N];
ll ans[N],cur;
void Add(int x)
{
for(;x!=rt;x=par[x])
{
if(was[x]) break;
was[x]=1;
Add(root,1,n,lid[x],rid[x],-dep[x]+dep[par[x]]);
cur+=dep[x]-dep[par[x]];
}
}
int main()
{
int q,u,v,c,d;
scanf("%i",&n);
ll z=0;
for(int i=1;i<n;i++)
{
scanf("%i %i %i %i",&u,&v,&c,&d);
E[u].pb({v,c});
E[v].pb({u,d});
z+=c+d;
}
DFS(1,0);
rt=1;
for(int i=1;i<=n;i++)
{
sum[i]+=all_up;
if(sum[i]>sum[rt]) rt=i;
}
DFS2(rt,0);
ans[1]=cur=sum[rt];
Build(root,1,n);
int fir=0,sec=0;
for(auto e:E[rt])
{
v=e.first;
if(dep[my[v]]>dep[fir]) sec=fir,fir=my[v];
else if(dep[my[v]]>dep[sec]) sec=my[v];
}
Add(fir);
if(sec) Add(sec);
ans[2]=cur;
for(int i=3;i<=n;i++)
{
Add(id[root]);
ans[i]=cur;
}
scanf("%i",&q);
while(q--)
{
int x;
scanf("%i",&x);
printf("%lld\n",z-ans[x]);
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'void Build(int&, int, int)':
designated_cities.cpp:51:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=ss+se>>1;
~~^~~
designated_cities.cpp: In function 'void Add(int, int, int, int, int, long long int)':
designated_cities.cpp:62:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=ss+se>>1;
~~^~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:85:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&n);
~~~~~^~~~~~~~~
designated_cities.cpp:89:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i %i %i",&u,&v,&c,&d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:119:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&q);
~~~~~^~~~~~~~~
designated_cities.cpp:123:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&x);
~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Incorrect |
6 ms |
5112 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
310 ms |
37504 KB |
Output is correct |
3 |
Correct |
451 ms |
46636 KB |
Output is correct |
4 |
Correct |
289 ms |
35436 KB |
Output is correct |
5 |
Correct |
303 ms |
36848 KB |
Output is correct |
6 |
Correct |
329 ms |
39928 KB |
Output is correct |
7 |
Correct |
257 ms |
36972 KB |
Output is correct |
8 |
Correct |
474 ms |
50040 KB |
Output is correct |
9 |
Correct |
212 ms |
37452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Incorrect |
335 ms |
37420 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Incorrect |
6 ms |
5112 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
310 ms |
37504 KB |
Output is correct |
3 |
Correct |
451 ms |
46636 KB |
Output is correct |
4 |
Correct |
289 ms |
35436 KB |
Output is correct |
5 |
Correct |
303 ms |
36848 KB |
Output is correct |
6 |
Correct |
329 ms |
39928 KB |
Output is correct |
7 |
Correct |
257 ms |
36972 KB |
Output is correct |
8 |
Correct |
474 ms |
50040 KB |
Output is correct |
9 |
Correct |
212 ms |
37452 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Incorrect |
335 ms |
37420 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Incorrect |
6 ms |
5112 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |