#include <bits/stdc++.h>
#define int long long
using namespace std;
int sum=0, m, n;
int u[200005], v[200005], a[200005],b[200005];
int tin[200005], tout[200005], pos[200005];
vector<int> adj[200005];
int ans[200005];
bool used[200005];
pair<int, int> trace2;
int f[200005];
int lazy[800005];
pair<int, int> it[800005];
int now=0, dora;
void down(int id){
it[id*2].first+=lazy[id];
it[id*2+1].first+=lazy[id];
lazy[id*2]+=lazy[id];
lazy[id*2+1]+=lazy[id];
lazy[id]=0;
}
void update(int id, int l, int r, int u, int v, int val){
if(l>v||r<u) return;
if(l>=u&&r<=v)
{
it[id].first+=val;
lazy[id]+=val;
return;
}
down(id);
int mid=(l+r)/2;
update(id*2, l, mid, u, v, val);
update(id*2+1, mid+1, r, u, v, val);
it[id]=max(it[id*2], it[id*2+1]);
}
pair<int, int> get(int id, int l, int r, int u, int v){
if(l>v||r<u) return{-1, -1};
if(l>=u&&r<=v)
{
return it[id];
}
down(id);
int mid=(l+r)/2;
return max(get(id*2, l, mid, u, v), get(id*2+1, mid+1, r, u, v));
}
pair<int, int> unpack(int node, int i){
int uv=a[i], vu=b[i];
if(node!=u[i])
{
swap(uv, vu);
}
return {uv, vu};
}
void dfs(int node, int fa){
f[node]=fa;
dora++;
tin[node]=dora;
pos[dora]=node;
for(auto i:adj[node])
{
int other=u[i]+v[i]-node, uv, vu;
if(other==fa) continue;
tie(uv, vu)=unpack(node, i);
dfs(other, node);
}
tout[node]=dora;
for(auto i:adj[node])
{
int other=u[i]+v[i]-node, uv, vu;
if(other==fa) continue;
tie(uv, vu)=unpack(node, i);
update(1, 1, n, tin[other], tout[other], uv);
now+=vu;
}
}
void dfsget(int node, int fa){
int w, oth;
ans[1]=max(ans[1], now);
tie(w, oth)=it[1];
if(ans[2]<w+now)
{
// cout<<w<<" "<<now<<endl;
ans[2]=w+now;
trace2={node, oth};
}
for(auto i:adj[node])
{
int other=u[i]+v[i]-node, uv, vu;
if(other==fa) continue;
tie(uv, vu)=unpack(node, i);
now+=uv-vu;
update(1, 1, n, 1, n, vu);
update(1, 1, n, tin[other], tout[other], -vu-uv);
dfsget(other, node);
now-=uv-vu;
update(1, 1, n, 1, n, -vu);
update(1, 1, n, tin[other], tout[other], +vu+uv);
}
}
void rebuild(int id, int l, int r){
if(l==r)
{
it[id]={0, pos[l]};
lazy[id]=0;
}
else
{
int mid=(l+r)/2;
rebuild(id*2, l, mid);
rebuild(id*2+1, mid+1, r);
it[id]=min(it[id*2], it[id*2+1]);
lazy[id]=0;
}
}
signed main()
{
ios::sync_with_stdio(false);
cin>>n;
for(int i=1; i<n; i++)
{
cin>>u[i]>>v[i]>>a[i]>>b[i];
sum+=a[i]+b[i];
adj[u[i]].push_back(i);
adj[v[i]].push_back(i);
}
dora=0, now=0;
dfs(1, 1);
rebuild(1, 1, n);
dora=0, now=0;
dfs(1, 1);
dfsget(1, 1);
// cout<<"wow shit this happened"<<endl;
dora=0, now=0;
dfs(trace2.first, trace2.first);
rebuild(1, 1, n);
dora=0, now=0;
dfs(trace2.first, trace2.first);
int lfcnt=0;
for(int i=1; i<=n; i++)
{
if(adj[i].size()==1)
{
lfcnt++;
}
}
used[trace2.first]=true;
// for(int i=1; i<=4; i++)
// {
// cout<<f[i]<<endl;
// }
// return 0;
for(int i=2; i<=lfcnt; i++)
{
ans[i]=ans[i-1];
if(i==2) ans[i]=now;
int w, oth;
tie(w, oth)=it[1];
while(!used[oth])
{
// cout<<oth<<endl;
for(auto j:adj[oth])
{
if(u[j]+v[j]-oth==f[oth])
{
int uv, vu;
tie(uv, vu)=unpack(oth, j);
update(1, 1, n, tin[oth], tout[oth], -vu);
ans[i]+=vu;
used[oth]=true;
break;
}
}
oth=f[oth];
}
}
// cout<<"how the fuck did my code even run to this point"<<endl;
int q;
cin>>q;
for(int i=1; i<=q; i++)
{
int ax;
cin>>ax;
if(ax>lfcnt) ax=lfcnt;
cout<<sum-ans[ax]<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5116 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5160 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
5088 KB |
Output is correct |
8 |
Correct |
6 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
5056 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
5112 KB |
Output is correct |
2 |
Correct |
1339 ms |
38256 KB |
Output is correct |
3 |
Correct |
1563 ms |
51704 KB |
Output is correct |
4 |
Correct |
1294 ms |
38092 KB |
Output is correct |
5 |
Correct |
1316 ms |
38224 KB |
Output is correct |
6 |
Correct |
1407 ms |
40272 KB |
Output is correct |
7 |
Correct |
1194 ms |
38636 KB |
Output is correct |
8 |
Correct |
1747 ms |
52200 KB |
Output is correct |
9 |
Correct |
982 ms |
39432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
1336 ms |
38152 KB |
Output is correct |
3 |
Correct |
1500 ms |
60784 KB |
Output is correct |
4 |
Correct |
1298 ms |
43168 KB |
Output is correct |
5 |
Correct |
1298 ms |
44656 KB |
Output is correct |
6 |
Correct |
1421 ms |
46976 KB |
Output is correct |
7 |
Correct |
1106 ms |
45668 KB |
Output is correct |
8 |
Correct |
1663 ms |
53880 KB |
Output is correct |
9 |
Correct |
1014 ms |
45724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5116 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5160 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
5088 KB |
Output is correct |
8 |
Correct |
6 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
5056 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
18 ms |
5496 KB |
Output is correct |
14 |
Correct |
17 ms |
5624 KB |
Output is correct |
15 |
Correct |
18 ms |
5496 KB |
Output is correct |
16 |
Correct |
18 ms |
5496 KB |
Output is correct |
17 |
Correct |
17 ms |
5496 KB |
Output is correct |
18 |
Correct |
18 ms |
5496 KB |
Output is correct |
19 |
Correct |
18 ms |
5496 KB |
Output is correct |
20 |
Correct |
19 ms |
5496 KB |
Output is correct |
21 |
Correct |
18 ms |
5496 KB |
Output is correct |
22 |
Correct |
18 ms |
5496 KB |
Output is correct |
23 |
Correct |
18 ms |
5628 KB |
Output is correct |
24 |
Correct |
17 ms |
5624 KB |
Output is correct |
25 |
Correct |
19 ms |
5624 KB |
Output is correct |
26 |
Correct |
17 ms |
5496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
5112 KB |
Output is correct |
2 |
Correct |
1339 ms |
38256 KB |
Output is correct |
3 |
Correct |
1563 ms |
51704 KB |
Output is correct |
4 |
Correct |
1294 ms |
38092 KB |
Output is correct |
5 |
Correct |
1316 ms |
38224 KB |
Output is correct |
6 |
Correct |
1407 ms |
40272 KB |
Output is correct |
7 |
Correct |
1194 ms |
38636 KB |
Output is correct |
8 |
Correct |
1747 ms |
52200 KB |
Output is correct |
9 |
Correct |
982 ms |
39432 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
1336 ms |
38152 KB |
Output is correct |
12 |
Correct |
1500 ms |
60784 KB |
Output is correct |
13 |
Correct |
1298 ms |
43168 KB |
Output is correct |
14 |
Correct |
1298 ms |
44656 KB |
Output is correct |
15 |
Correct |
1421 ms |
46976 KB |
Output is correct |
16 |
Correct |
1106 ms |
45668 KB |
Output is correct |
17 |
Correct |
1663 ms |
53880 KB |
Output is correct |
18 |
Correct |
1014 ms |
45724 KB |
Output is correct |
19 |
Correct |
6 ms |
5112 KB |
Output is correct |
20 |
Correct |
1345 ms |
44596 KB |
Output is correct |
21 |
Correct |
1869 ms |
60792 KB |
Output is correct |
22 |
Correct |
1332 ms |
43144 KB |
Output is correct |
23 |
Correct |
1330 ms |
44636 KB |
Output is correct |
24 |
Correct |
1313 ms |
43384 KB |
Output is correct |
25 |
Correct |
1314 ms |
44664 KB |
Output is correct |
26 |
Correct |
1312 ms |
43384 KB |
Output is correct |
27 |
Correct |
1272 ms |
44680 KB |
Output is correct |
28 |
Correct |
1393 ms |
46464 KB |
Output is correct |
29 |
Correct |
1365 ms |
44492 KB |
Output is correct |
30 |
Correct |
1280 ms |
43888 KB |
Output is correct |
31 |
Correct |
1190 ms |
45164 KB |
Output is correct |
32 |
Correct |
1614 ms |
54264 KB |
Output is correct |
33 |
Correct |
1012 ms |
46160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5116 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5160 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
5088 KB |
Output is correct |
8 |
Correct |
6 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
5056 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
15 ms |
5112 KB |
Output is correct |
13 |
Correct |
1339 ms |
38256 KB |
Output is correct |
14 |
Correct |
1563 ms |
51704 KB |
Output is correct |
15 |
Correct |
1294 ms |
38092 KB |
Output is correct |
16 |
Correct |
1316 ms |
38224 KB |
Output is correct |
17 |
Correct |
1407 ms |
40272 KB |
Output is correct |
18 |
Correct |
1194 ms |
38636 KB |
Output is correct |
19 |
Correct |
1747 ms |
52200 KB |
Output is correct |
20 |
Correct |
982 ms |
39432 KB |
Output is correct |
21 |
Correct |
6 ms |
5112 KB |
Output is correct |
22 |
Correct |
1336 ms |
38152 KB |
Output is correct |
23 |
Correct |
1500 ms |
60784 KB |
Output is correct |
24 |
Correct |
1298 ms |
43168 KB |
Output is correct |
25 |
Correct |
1298 ms |
44656 KB |
Output is correct |
26 |
Correct |
1421 ms |
46976 KB |
Output is correct |
27 |
Correct |
1106 ms |
45668 KB |
Output is correct |
28 |
Correct |
1663 ms |
53880 KB |
Output is correct |
29 |
Correct |
1014 ms |
45724 KB |
Output is correct |
30 |
Correct |
6 ms |
5112 KB |
Output is correct |
31 |
Correct |
18 ms |
5496 KB |
Output is correct |
32 |
Correct |
17 ms |
5624 KB |
Output is correct |
33 |
Correct |
18 ms |
5496 KB |
Output is correct |
34 |
Correct |
18 ms |
5496 KB |
Output is correct |
35 |
Correct |
17 ms |
5496 KB |
Output is correct |
36 |
Correct |
18 ms |
5496 KB |
Output is correct |
37 |
Correct |
18 ms |
5496 KB |
Output is correct |
38 |
Correct |
19 ms |
5496 KB |
Output is correct |
39 |
Correct |
18 ms |
5496 KB |
Output is correct |
40 |
Correct |
18 ms |
5496 KB |
Output is correct |
41 |
Correct |
18 ms |
5628 KB |
Output is correct |
42 |
Correct |
17 ms |
5624 KB |
Output is correct |
43 |
Correct |
19 ms |
5624 KB |
Output is correct |
44 |
Correct |
17 ms |
5496 KB |
Output is correct |
45 |
Correct |
6 ms |
5112 KB |
Output is correct |
46 |
Correct |
1345 ms |
44596 KB |
Output is correct |
47 |
Correct |
1869 ms |
60792 KB |
Output is correct |
48 |
Correct |
1332 ms |
43144 KB |
Output is correct |
49 |
Correct |
1330 ms |
44636 KB |
Output is correct |
50 |
Correct |
1313 ms |
43384 KB |
Output is correct |
51 |
Correct |
1314 ms |
44664 KB |
Output is correct |
52 |
Correct |
1312 ms |
43384 KB |
Output is correct |
53 |
Correct |
1272 ms |
44680 KB |
Output is correct |
54 |
Correct |
1393 ms |
46464 KB |
Output is correct |
55 |
Correct |
1365 ms |
44492 KB |
Output is correct |
56 |
Correct |
1280 ms |
43888 KB |
Output is correct |
57 |
Correct |
1190 ms |
45164 KB |
Output is correct |
58 |
Correct |
1614 ms |
54264 KB |
Output is correct |
59 |
Correct |
1012 ms |
46160 KB |
Output is correct |
60 |
Correct |
6 ms |
5112 KB |
Output is correct |
61 |
Correct |
1916 ms |
47296 KB |
Output is correct |
62 |
Execution timed out |
2073 ms |
59828 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |