# include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define int long long
#define pii pair <int, int>
using namespace std;
const int N = 3e5 + 5;
int t,n,a[N],in[N],out[N],lv[N],sum,tin,b[N],c[N],d[N],up[N],vert,ver[N];
int fix[N],par[N];
int ans[N];
vector <pair <int , pii > > v[N];
struct nod{
int idx;
int mx;
};
nod tree[4*N],emp;
int lazy[4*N];
nod merge(nod a, nod b) {
if (a.mx >= b.mx) return a;
else return b;
}
void build(int node, int le, int ri) {
lazy[node] = 0;
if (le == ri) {
tree[node].idx = le;
tree[node].mx = 0;
return ;
}
int mid = (le + ri) / 2;
build(2*node,le,mid);
build(2*node + 1, mid + 1, ri);
tree[node] = merge(tree[2*node], tree[2*node + 1]);
}
void update(int node,int le,int ri,int start,int end,int val) {
if(lazy[node]) {
tree[node].mx += lazy[node];
if(le != ri) {
lazy[2*node] += lazy[node];
lazy[2*node+1] += lazy[node];
}
lazy[node]=0;
}
if(le>end || ri<start)
return;
if(start <= le && ri <= end) {
tree[node].mx += val;
if(le != ri) {
lazy[2*node] += val;
lazy[2*node+1] += val;
}
return;
}
int mid = (le + ri) / 2;
update(2*node,le,mid,start,end,val);
update(2*node+1,mid+1,ri,start,end,val);
tree[node] = merge(tree[2*node], tree[2*node+1]);
}
nod getans(int node,int le,int ri,int start,int end) {
if(lazy[node]) {
tree[node].mx += lazy[node];
if(le != ri) {
lazy[2*node] += lazy[node];
lazy[2*node+1] += lazy[node];
}
lazy[node] = 0;
}
if(le > end || ri < start) return emp;
if(start <= le && ri <= end) {
return tree[node];
}
int mid=( le + ri) / 2;
return merge(getans(2*node,le,mid,start,end), getans(2*node+1,mid+1,ri,start,end));
}
void dfs(int a, int p,int cost, int cost1) {
tin++;
in[a] = tin;
ver[in[a]] = a;
//rootidan a shi rom chamoxvide
par[a] = p;
up[a] = cost;
sum += cost;
lv[a] = lv[p] + cost;
if (vert) {
if (v[a].size() == 1 && a != vert) {
//if (a == 2) cout<<lv[a]<<endl<<endl<<endl;
// cout<<a<<" "<<in[a]<<" "<<lv[a]<<endl;
update(1,1,n,in[a],in[a],lv[a]);
}
} else
update(1,1,n,in[a],in[a],lv[a]);
for (int i = 0; i < v[a].size(); i++) {
int to = v[a][i].f; int froma = v[a][i].s.f; int fromto = v[a][i].s.s;
if (to == p) continue;
//if (to == 1 && vert) cout<<"asd"<<endl;
dfs(to,a,froma,fromto);
}
out[a] = tin;
}
void dfs1(int a, int p) {
ans[1] = min(ans[1], sum);
if (ans[2] > sum - getans(1,1,n,1,n).mx) {
vert = a;
ans[2] = min(ans[2],sum - getans(1,1,n,1,n).mx);
}
for (int i = 0; i < v[a].size(); i++) {
int to = v[a][i].f; int froma = v[a][i].s.f; int fromto = v[a][i].s.s;
if (to == p) continue;
sum -= froma; sum += fromto;
update(1,1,n,in[to], out[to],-froma);
update(1,1,n,1,in[to] - 1, fromto);
update(1,1,n,out[to] + 1, n, fromto);
dfs1(to,a);
sum += froma; sum -= fromto;
update(1,1,n,in[to], out[to], froma);
update(1,1,n,1,in[to] - 1, -fromto);
update(1,1,n,out[to] + 1, n, -fromto);
}
}
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl '\n'
emp = {0,0};
cin>>n;
for (int i = 1; i <= n - 1; i++) {
cin>>a[i]>>b[i]>>c[i]>>d[i];
v[a[i]].pb({b[i],{c[i],d[i]}});
v[b[i]].pb({a[i],{d[i],c[i]}});
}
ans[2] = ans[1] = 1e15;
build(1,1,n);
dfs(1,0,0,0);// cout<<sum<<endl;
//cout<<getans(1,1,n,1,n)<<endl;
dfs1(1,0);
//cout<<ans[1]<<endl;
//cout<<ans[2]<<endl;
sum = 0;
tin = 0;
build(1,1,n);
//cout<<vert<<endl;
dfs(vert,0,0,0);
for (int i = 2; i <= n; i++) {
int x = getans(1,1,n,1,n).mx;
int idx = ver[getans(1,1,n,1,n).idx];
//cout<<i<<" "<<idx<<" "<<x<<endl;
sum -= x;
while (!fix[idx] && idx != vert) {
update(1,1,n,in[idx], out[idx], -up[idx]);
fix[idx] = 1;
idx = par[idx];
}
ans[i] = sum;
// cout<<ans[i]<<endl;
}int q;cin>>q;
while (q--) {
int x;
cin>>x;
cout<<ans[x]<<endl;
}
}
/*
15
14 5 12 7
14 12 6 5
14 10 14 16
9 14 16 12
13 7 4 15
1 3 8 1
6 7 15 13
15 4 4 6
9 1 12 6
13 1 7 6
13 4 5 15
2 6 11 19
8 4 12 7
13 11 14 5
13
1
2
3
4
5
6
7
8
9
10
11
12
13
*/
Compilation message
designated_cities.cpp: In function 'void dfs(long long int, long long int, long long int, long long int)':
designated_cities.cpp:93:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for (int i = 0; i < v[a].size(); i++) {
| ~~^~~~~~~~~~~~~
designated_cities.cpp: In function 'void dfs1(long long int, long long int)':
designated_cities.cpp:107:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for (int i = 0; i < v[a].size(); i++) {
| ~~^~~~~~~~~~~~~
designated_cities.cpp: At global scope:
designated_cities.cpp:121:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
121 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7376 KB |
Output is correct |
4 |
Correct |
4 ms |
7384 KB |
Output is correct |
5 |
Correct |
4 ms |
7384 KB |
Output is correct |
6 |
Correct |
6 ms |
7372 KB |
Output is correct |
7 |
Correct |
5 ms |
7372 KB |
Output is correct |
8 |
Correct |
5 ms |
7384 KB |
Output is correct |
9 |
Correct |
6 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
4 ms |
7384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
782 ms |
57928 KB |
Output is correct |
3 |
Correct |
794 ms |
73340 KB |
Output is correct |
4 |
Correct |
785 ms |
56624 KB |
Output is correct |
5 |
Correct |
769 ms |
57684 KB |
Output is correct |
6 |
Correct |
782 ms |
60344 KB |
Output is correct |
7 |
Correct |
750 ms |
56776 KB |
Output is correct |
8 |
Correct |
778 ms |
73840 KB |
Output is correct |
9 |
Correct |
655 ms |
55880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
815 ms |
57964 KB |
Output is correct |
3 |
Correct |
618 ms |
75856 KB |
Output is correct |
4 |
Correct |
745 ms |
56636 KB |
Output is correct |
5 |
Correct |
724 ms |
57812 KB |
Output is correct |
6 |
Correct |
745 ms |
60660 KB |
Output is correct |
7 |
Correct |
696 ms |
55896 KB |
Output is correct |
8 |
Correct |
814 ms |
68120 KB |
Output is correct |
9 |
Correct |
650 ms |
55544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7376 KB |
Output is correct |
4 |
Correct |
4 ms |
7384 KB |
Output is correct |
5 |
Correct |
4 ms |
7384 KB |
Output is correct |
6 |
Correct |
6 ms |
7372 KB |
Output is correct |
7 |
Correct |
5 ms |
7372 KB |
Output is correct |
8 |
Correct |
5 ms |
7384 KB |
Output is correct |
9 |
Correct |
6 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
4 ms |
7384 KB |
Output is correct |
12 |
Correct |
4 ms |
7372 KB |
Output is correct |
13 |
Correct |
8 ms |
7904 KB |
Output is correct |
14 |
Correct |
10 ms |
8080 KB |
Output is correct |
15 |
Correct |
8 ms |
7884 KB |
Output is correct |
16 |
Correct |
8 ms |
7864 KB |
Output is correct |
17 |
Correct |
8 ms |
7864 KB |
Output is correct |
18 |
Correct |
8 ms |
7884 KB |
Output is correct |
19 |
Correct |
10 ms |
7904 KB |
Output is correct |
20 |
Correct |
8 ms |
7896 KB |
Output is correct |
21 |
Correct |
9 ms |
7872 KB |
Output is correct |
22 |
Correct |
8 ms |
7896 KB |
Output is correct |
23 |
Correct |
8 ms |
7904 KB |
Output is correct |
24 |
Correct |
8 ms |
7896 KB |
Output is correct |
25 |
Correct |
10 ms |
8004 KB |
Output is correct |
26 |
Correct |
8 ms |
7900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
782 ms |
57928 KB |
Output is correct |
3 |
Correct |
794 ms |
73340 KB |
Output is correct |
4 |
Correct |
785 ms |
56624 KB |
Output is correct |
5 |
Correct |
769 ms |
57684 KB |
Output is correct |
6 |
Correct |
782 ms |
60344 KB |
Output is correct |
7 |
Correct |
750 ms |
56776 KB |
Output is correct |
8 |
Correct |
778 ms |
73840 KB |
Output is correct |
9 |
Correct |
655 ms |
55880 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
815 ms |
57964 KB |
Output is correct |
12 |
Correct |
618 ms |
75856 KB |
Output is correct |
13 |
Correct |
745 ms |
56636 KB |
Output is correct |
14 |
Correct |
724 ms |
57812 KB |
Output is correct |
15 |
Correct |
745 ms |
60660 KB |
Output is correct |
16 |
Correct |
696 ms |
55896 KB |
Output is correct |
17 |
Correct |
814 ms |
68120 KB |
Output is correct |
18 |
Correct |
650 ms |
55544 KB |
Output is correct |
19 |
Correct |
4 ms |
7384 KB |
Output is correct |
20 |
Correct |
744 ms |
58160 KB |
Output is correct |
21 |
Correct |
739 ms |
76072 KB |
Output is correct |
22 |
Correct |
686 ms |
56572 KB |
Output is correct |
23 |
Correct |
729 ms |
58300 KB |
Output is correct |
24 |
Correct |
715 ms |
57028 KB |
Output is correct |
25 |
Correct |
728 ms |
58308 KB |
Output is correct |
26 |
Correct |
745 ms |
57016 KB |
Output is correct |
27 |
Correct |
710 ms |
57728 KB |
Output is correct |
28 |
Correct |
755 ms |
60228 KB |
Output is correct |
29 |
Correct |
718 ms |
58564 KB |
Output is correct |
30 |
Correct |
749 ms |
56640 KB |
Output is correct |
31 |
Correct |
728 ms |
56576 KB |
Output is correct |
32 |
Correct |
790 ms |
68888 KB |
Output is correct |
33 |
Correct |
655 ms |
55924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7376 KB |
Output is correct |
4 |
Correct |
4 ms |
7384 KB |
Output is correct |
5 |
Correct |
4 ms |
7384 KB |
Output is correct |
6 |
Correct |
6 ms |
7372 KB |
Output is correct |
7 |
Correct |
5 ms |
7372 KB |
Output is correct |
8 |
Correct |
5 ms |
7384 KB |
Output is correct |
9 |
Correct |
6 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
4 ms |
7384 KB |
Output is correct |
12 |
Correct |
4 ms |
7372 KB |
Output is correct |
13 |
Correct |
782 ms |
57928 KB |
Output is correct |
14 |
Correct |
794 ms |
73340 KB |
Output is correct |
15 |
Correct |
785 ms |
56624 KB |
Output is correct |
16 |
Correct |
769 ms |
57684 KB |
Output is correct |
17 |
Correct |
782 ms |
60344 KB |
Output is correct |
18 |
Correct |
750 ms |
56776 KB |
Output is correct |
19 |
Correct |
778 ms |
73840 KB |
Output is correct |
20 |
Correct |
655 ms |
55880 KB |
Output is correct |
21 |
Correct |
4 ms |
7372 KB |
Output is correct |
22 |
Correct |
815 ms |
57964 KB |
Output is correct |
23 |
Correct |
618 ms |
75856 KB |
Output is correct |
24 |
Correct |
745 ms |
56636 KB |
Output is correct |
25 |
Correct |
724 ms |
57812 KB |
Output is correct |
26 |
Correct |
745 ms |
60660 KB |
Output is correct |
27 |
Correct |
696 ms |
55896 KB |
Output is correct |
28 |
Correct |
814 ms |
68120 KB |
Output is correct |
29 |
Correct |
650 ms |
55544 KB |
Output is correct |
30 |
Correct |
4 ms |
7372 KB |
Output is correct |
31 |
Correct |
8 ms |
7904 KB |
Output is correct |
32 |
Correct |
10 ms |
8080 KB |
Output is correct |
33 |
Correct |
8 ms |
7884 KB |
Output is correct |
34 |
Correct |
8 ms |
7864 KB |
Output is correct |
35 |
Correct |
8 ms |
7864 KB |
Output is correct |
36 |
Correct |
8 ms |
7884 KB |
Output is correct |
37 |
Correct |
10 ms |
7904 KB |
Output is correct |
38 |
Correct |
8 ms |
7896 KB |
Output is correct |
39 |
Correct |
9 ms |
7872 KB |
Output is correct |
40 |
Correct |
8 ms |
7896 KB |
Output is correct |
41 |
Correct |
8 ms |
7904 KB |
Output is correct |
42 |
Correct |
8 ms |
7896 KB |
Output is correct |
43 |
Correct |
10 ms |
8004 KB |
Output is correct |
44 |
Correct |
8 ms |
7900 KB |
Output is correct |
45 |
Correct |
4 ms |
7384 KB |
Output is correct |
46 |
Correct |
744 ms |
58160 KB |
Output is correct |
47 |
Correct |
739 ms |
76072 KB |
Output is correct |
48 |
Correct |
686 ms |
56572 KB |
Output is correct |
49 |
Correct |
729 ms |
58300 KB |
Output is correct |
50 |
Correct |
715 ms |
57028 KB |
Output is correct |
51 |
Correct |
728 ms |
58308 KB |
Output is correct |
52 |
Correct |
745 ms |
57016 KB |
Output is correct |
53 |
Correct |
710 ms |
57728 KB |
Output is correct |
54 |
Correct |
755 ms |
60228 KB |
Output is correct |
55 |
Correct |
718 ms |
58564 KB |
Output is correct |
56 |
Correct |
749 ms |
56640 KB |
Output is correct |
57 |
Correct |
728 ms |
56576 KB |
Output is correct |
58 |
Correct |
790 ms |
68888 KB |
Output is correct |
59 |
Correct |
655 ms |
55924 KB |
Output is correct |
60 |
Correct |
4 ms |
7372 KB |
Output is correct |
61 |
Correct |
779 ms |
60756 KB |
Output is correct |
62 |
Correct |
676 ms |
75124 KB |
Output is correct |
63 |
Correct |
760 ms |
59340 KB |
Output is correct |
64 |
Correct |
813 ms |
60920 KB |
Output is correct |
65 |
Correct |
770 ms |
59496 KB |
Output is correct |
66 |
Correct |
763 ms |
60860 KB |
Output is correct |
67 |
Correct |
826 ms |
59608 KB |
Output is correct |
68 |
Correct |
778 ms |
60644 KB |
Output is correct |
69 |
Correct |
824 ms |
62788 KB |
Output is correct |
70 |
Correct |
761 ms |
61252 KB |
Output is correct |
71 |
Correct |
747 ms |
59344 KB |
Output is correct |
72 |
Correct |
722 ms |
60104 KB |
Output is correct |
73 |
Correct |
810 ms |
69552 KB |
Output is correct |
74 |
Correct |
700 ms |
60176 KB |
Output is correct |