#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
using namespace std;
const int mxn=2e5+5;
struct dat{
int v;ll a,b;
};vector<dat>g[mxn];
ll dp[mxn]{0},rs[mxn]{0},d[mxn]{0};
void dfs1(int u,int p){
for(auto [v,a,b]:g[u]){
if(v==p)continue;
dfs1(v,u);dp[u]+=b+dp[v];
}
}
void dfs2(int u,int p){
for(auto [v,a,b] :g[u]){
if(v==p)continue;
dp[v]=dp[u]+a-b;dfs2(v,u);
}
}int rt;
ll dfs3(int u,int p){
ll mx1=0,mx2=0;
for(auto [v,a,b] : g[u]){
if(v==p)continue;
dfs3(v,u);
ll tt=d[v]+a;
if(tt>mx1)swap(tt,mx1);
if(tt>mx2)swap(tt,mx2);
}
if(rs[2]<mx1+mx2+dp[u]){
rs[2]=mx1+mx2+dp[u];
rt=u;
}
return d[u]=mx1;
}
vector<ll>di[mxn];
void dfs4(int u,int p,ll t){
di[u].pb(0);
ll mx1=0,mx2=0;
for(auto [v,a,b] : g[u]){
if(v==p)continue;
dfs4(v,u,a);ll tt=di[v].back();
if(tt>mx1)swap(tt,mx1);
if(tt>mx2)swap(tt,mx2);
if(di[u].size()<di[v].size())swap(di[u],di[v]);
if(di[u].back()>di[v].back())swap(di[u].back(),di[v].back());
for(auto x:di[v])di[u].pb(x);
}di[u].back()+=t;
if(u==rt){
di[u].erase(find(di[u].begin(),di[u].end(),mx1));
di[u].erase(find(di[u].begin(),di[u].end(),mx2));
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n;cin>>n;ll ans=0;
for(int i=1;i<=n-1;i++){
int u,v,a,b;cin>>u>>v>>a>>b;ans+=a+b;
g[u].pb({v,a,b});g[v].pb({u,b,a});
}dfs1(1,1);dfs2(1,1);for(int i=1;i<=n;i++)rs[1]=max(rs[1],dp[i]);
dfs3(1,1);dfs4(rt,rt,0);sort(di[rt].rbegin(),di[rt].rend());
ll cur = rs[2]-dp[rt];
for(int i=0;i<di[rt].size();i++){
cur+=di[rt][i];
rs[i+3]=dp[rt]+cur;
}int q;cin>>q;
while(q--){
int x;cin>>x;
cout<<ans-rs[x]<<'\n';
}
}
Compilation message
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:73:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i=0;i<di[rt].size();i++){
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12380 KB |
Output is correct |
2 |
Correct |
3 ms |
12484 KB |
Output is correct |
3 |
Correct |
2 ms |
12376 KB |
Output is correct |
4 |
Correct |
2 ms |
12380 KB |
Output is correct |
5 |
Correct |
3 ms |
12380 KB |
Output is correct |
6 |
Correct |
3 ms |
12496 KB |
Output is correct |
7 |
Correct |
4 ms |
12380 KB |
Output is correct |
8 |
Correct |
2 ms |
12492 KB |
Output is correct |
9 |
Correct |
4 ms |
12380 KB |
Output is correct |
10 |
Correct |
3 ms |
12376 KB |
Output is correct |
11 |
Correct |
2 ms |
12376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12380 KB |
Output is correct |
2 |
Correct |
177 ms |
48560 KB |
Output is correct |
3 |
Correct |
264 ms |
62408 KB |
Output is correct |
4 |
Correct |
168 ms |
47556 KB |
Output is correct |
5 |
Correct |
169 ms |
43448 KB |
Output is correct |
6 |
Correct |
179 ms |
46784 KB |
Output is correct |
7 |
Correct |
149 ms |
41212 KB |
Output is correct |
8 |
Correct |
263 ms |
62372 KB |
Output is correct |
9 |
Correct |
108 ms |
39676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12376 KB |
Output is correct |
2 |
Correct |
180 ms |
48504 KB |
Output is correct |
3 |
Correct |
231 ms |
65592 KB |
Output is correct |
4 |
Correct |
192 ms |
47224 KB |
Output is correct |
5 |
Correct |
178 ms |
43436 KB |
Output is correct |
6 |
Correct |
183 ms |
47184 KB |
Output is correct |
7 |
Correct |
125 ms |
39928 KB |
Output is correct |
8 |
Correct |
190 ms |
55256 KB |
Output is correct |
9 |
Correct |
144 ms |
39744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12380 KB |
Output is correct |
2 |
Correct |
3 ms |
12484 KB |
Output is correct |
3 |
Correct |
2 ms |
12376 KB |
Output is correct |
4 |
Correct |
2 ms |
12380 KB |
Output is correct |
5 |
Correct |
3 ms |
12380 KB |
Output is correct |
6 |
Correct |
3 ms |
12496 KB |
Output is correct |
7 |
Correct |
4 ms |
12380 KB |
Output is correct |
8 |
Correct |
2 ms |
12492 KB |
Output is correct |
9 |
Correct |
4 ms |
12380 KB |
Output is correct |
10 |
Correct |
3 ms |
12376 KB |
Output is correct |
11 |
Correct |
2 ms |
12376 KB |
Output is correct |
12 |
Correct |
3 ms |
12380 KB |
Output is correct |
13 |
Correct |
4 ms |
12712 KB |
Output is correct |
14 |
Correct |
4 ms |
12892 KB |
Output is correct |
15 |
Correct |
5 ms |
12632 KB |
Output is correct |
16 |
Correct |
4 ms |
12856 KB |
Output is correct |
17 |
Correct |
3 ms |
12636 KB |
Output is correct |
18 |
Correct |
4 ms |
12636 KB |
Output is correct |
19 |
Correct |
4 ms |
12716 KB |
Output is correct |
20 |
Correct |
4 ms |
12832 KB |
Output is correct |
21 |
Correct |
4 ms |
12636 KB |
Output is correct |
22 |
Correct |
4 ms |
12636 KB |
Output is correct |
23 |
Correct |
4 ms |
12636 KB |
Output is correct |
24 |
Correct |
4 ms |
12636 KB |
Output is correct |
25 |
Correct |
5 ms |
13148 KB |
Output is correct |
26 |
Correct |
4 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12380 KB |
Output is correct |
2 |
Correct |
177 ms |
48560 KB |
Output is correct |
3 |
Correct |
264 ms |
62408 KB |
Output is correct |
4 |
Correct |
168 ms |
47556 KB |
Output is correct |
5 |
Correct |
169 ms |
43448 KB |
Output is correct |
6 |
Correct |
179 ms |
46784 KB |
Output is correct |
7 |
Correct |
149 ms |
41212 KB |
Output is correct |
8 |
Correct |
263 ms |
62372 KB |
Output is correct |
9 |
Correct |
108 ms |
39676 KB |
Output is correct |
10 |
Correct |
3 ms |
12376 KB |
Output is correct |
11 |
Correct |
180 ms |
48504 KB |
Output is correct |
12 |
Correct |
231 ms |
65592 KB |
Output is correct |
13 |
Correct |
192 ms |
47224 KB |
Output is correct |
14 |
Correct |
178 ms |
43436 KB |
Output is correct |
15 |
Correct |
183 ms |
47184 KB |
Output is correct |
16 |
Correct |
125 ms |
39928 KB |
Output is correct |
17 |
Correct |
190 ms |
55256 KB |
Output is correct |
18 |
Correct |
144 ms |
39744 KB |
Output is correct |
19 |
Correct |
4 ms |
12380 KB |
Output is correct |
20 |
Correct |
189 ms |
48324 KB |
Output is correct |
21 |
Correct |
268 ms |
65484 KB |
Output is correct |
22 |
Correct |
164 ms |
47300 KB |
Output is correct |
23 |
Correct |
235 ms |
48320 KB |
Output is correct |
24 |
Correct |
179 ms |
47812 KB |
Output is correct |
25 |
Correct |
182 ms |
48832 KB |
Output is correct |
26 |
Correct |
184 ms |
47296 KB |
Output is correct |
27 |
Correct |
154 ms |
43516 KB |
Output is correct |
28 |
Correct |
191 ms |
46760 KB |
Output is correct |
29 |
Correct |
183 ms |
48100 KB |
Output is correct |
30 |
Correct |
156 ms |
46336 KB |
Output is correct |
31 |
Correct |
149 ms |
40600 KB |
Output is correct |
32 |
Correct |
199 ms |
55988 KB |
Output is correct |
33 |
Correct |
122 ms |
39672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12380 KB |
Output is correct |
2 |
Correct |
3 ms |
12484 KB |
Output is correct |
3 |
Correct |
2 ms |
12376 KB |
Output is correct |
4 |
Correct |
2 ms |
12380 KB |
Output is correct |
5 |
Correct |
3 ms |
12380 KB |
Output is correct |
6 |
Correct |
3 ms |
12496 KB |
Output is correct |
7 |
Correct |
4 ms |
12380 KB |
Output is correct |
8 |
Correct |
2 ms |
12492 KB |
Output is correct |
9 |
Correct |
4 ms |
12380 KB |
Output is correct |
10 |
Correct |
3 ms |
12376 KB |
Output is correct |
11 |
Correct |
2 ms |
12376 KB |
Output is correct |
12 |
Correct |
2 ms |
12380 KB |
Output is correct |
13 |
Correct |
177 ms |
48560 KB |
Output is correct |
14 |
Correct |
264 ms |
62408 KB |
Output is correct |
15 |
Correct |
168 ms |
47556 KB |
Output is correct |
16 |
Correct |
169 ms |
43448 KB |
Output is correct |
17 |
Correct |
179 ms |
46784 KB |
Output is correct |
18 |
Correct |
149 ms |
41212 KB |
Output is correct |
19 |
Correct |
263 ms |
62372 KB |
Output is correct |
20 |
Correct |
108 ms |
39676 KB |
Output is correct |
21 |
Correct |
3 ms |
12376 KB |
Output is correct |
22 |
Correct |
180 ms |
48504 KB |
Output is correct |
23 |
Correct |
231 ms |
65592 KB |
Output is correct |
24 |
Correct |
192 ms |
47224 KB |
Output is correct |
25 |
Correct |
178 ms |
43436 KB |
Output is correct |
26 |
Correct |
183 ms |
47184 KB |
Output is correct |
27 |
Correct |
125 ms |
39928 KB |
Output is correct |
28 |
Correct |
190 ms |
55256 KB |
Output is correct |
29 |
Correct |
144 ms |
39744 KB |
Output is correct |
30 |
Correct |
3 ms |
12380 KB |
Output is correct |
31 |
Correct |
4 ms |
12712 KB |
Output is correct |
32 |
Correct |
4 ms |
12892 KB |
Output is correct |
33 |
Correct |
5 ms |
12632 KB |
Output is correct |
34 |
Correct |
4 ms |
12856 KB |
Output is correct |
35 |
Correct |
3 ms |
12636 KB |
Output is correct |
36 |
Correct |
4 ms |
12636 KB |
Output is correct |
37 |
Correct |
4 ms |
12716 KB |
Output is correct |
38 |
Correct |
4 ms |
12832 KB |
Output is correct |
39 |
Correct |
4 ms |
12636 KB |
Output is correct |
40 |
Correct |
4 ms |
12636 KB |
Output is correct |
41 |
Correct |
4 ms |
12636 KB |
Output is correct |
42 |
Correct |
4 ms |
12636 KB |
Output is correct |
43 |
Correct |
5 ms |
13148 KB |
Output is correct |
44 |
Correct |
4 ms |
12636 KB |
Output is correct |
45 |
Correct |
4 ms |
12380 KB |
Output is correct |
46 |
Correct |
189 ms |
48324 KB |
Output is correct |
47 |
Correct |
268 ms |
65484 KB |
Output is correct |
48 |
Correct |
164 ms |
47300 KB |
Output is correct |
49 |
Correct |
235 ms |
48320 KB |
Output is correct |
50 |
Correct |
179 ms |
47812 KB |
Output is correct |
51 |
Correct |
182 ms |
48832 KB |
Output is correct |
52 |
Correct |
184 ms |
47296 KB |
Output is correct |
53 |
Correct |
154 ms |
43516 KB |
Output is correct |
54 |
Correct |
191 ms |
46760 KB |
Output is correct |
55 |
Correct |
183 ms |
48100 KB |
Output is correct |
56 |
Correct |
156 ms |
46336 KB |
Output is correct |
57 |
Correct |
149 ms |
40600 KB |
Output is correct |
58 |
Correct |
199 ms |
55988 KB |
Output is correct |
59 |
Correct |
122 ms |
39672 KB |
Output is correct |
60 |
Correct |
3 ms |
12376 KB |
Output is correct |
61 |
Correct |
219 ms |
51860 KB |
Output is correct |
62 |
Correct |
243 ms |
63840 KB |
Output is correct |
63 |
Correct |
179 ms |
50328 KB |
Output is correct |
64 |
Correct |
199 ms |
52160 KB |
Output is correct |
65 |
Correct |
178 ms |
50032 KB |
Output is correct |
66 |
Correct |
191 ms |
51392 KB |
Output is correct |
67 |
Correct |
181 ms |
50108 KB |
Output is correct |
68 |
Correct |
199 ms |
46584 KB |
Output is correct |
69 |
Correct |
210 ms |
49160 KB |
Output is correct |
70 |
Correct |
219 ms |
50816 KB |
Output is correct |
71 |
Correct |
172 ms |
49064 KB |
Output is correct |
72 |
Correct |
165 ms |
44288 KB |
Output is correct |
73 |
Correct |
236 ms |
56348 KB |
Output is correct |
74 |
Correct |
137 ms |
44020 KB |
Output is correct |