#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define pb push_back
#define all(x) x.begin() , x.end()
const int N = 3e5 + 10;
const int INF = 1e18 + 10;
const int mod = 1e9 + 7;
int n;
vector<pair<int , pair<int , int>>> g[N];
int ans[N];
int dp[N];
int dw[N];
int up[N];
vector<int> pre[N];
vector<int> suf[N];
int pos[N];
void efs(int v , int pr) {
for(auto [u , w] : g[v]) if(u != pr) {
efs(u , v);
dp[v] += dp[u] + w.S;
}
}
int root;
void rer(int v , int pr) {
ans[1] = max(ans[1] , dp[v]);
// cout << v << " : " << dp[v] << " " << max(dw[v] , up[v]) << " tet\n";
if(ans[2] < dp[v] + max(dw[v] , up[v])) {ans[2] = dp[v] + max(dw[v] , up[v]) , root = v;}
for(auto [u , w] : g[v]) if(u != pr) {
dp[v] -= dp[u] + w.S;
dp[u] += dp[v] + w.F;
rer(u , v);
dp[u] -= dp[v] + w.F;
dp[v] += dp[u] + w.S;
}
}
void kfs(int v , int pr) {
for(auto [u , w] : g[v]) if(u != pr) {
kfs(u , v);
dw[v] = max(dw[v] , dw[u] + w.F);
pos[u] = pre[v].size();
pre[v].pb(dw[v]);
}
for(int i = g[v].size()-1 ; ~i ; i--) {
int u = g[v][i].F;
auto w = g[v][i].S;
if(u == pr) continue;
int x = dw[u] + w.F;
if(suf[v].size()) suf[v].pb(max(suf[v].back() , x));
else suf[v].pb(x);
}
reverse(all(suf[v]));
}
void lfs(int v , int pr) {
for(auto [u , w] : g[v]) if(u != pr) {
up[u] = up[v];
int p = pos[u];
if(p != 0) up[u] = max(up[u] , pre[v][p-1]);
if(p != suf[v].size() - 1) up[u] = max(up[u] , suf[v][p+1]);
up[u] += w.S;
lfs(u , v);
}
}
multiset<int> s[N];
void dfs(int v , int pr) {
s[v].insert(0);
for(auto [u , w] : g[v]) if(u != pr) {
dfs(u , v);
int x = *(--s[u].end());
s[u].erase(--s[u].end());
s[u].insert(x + w.F);
if(s[u].size() > s[v].size()) s[v].swap(s[u]);
for(auto p : s[u]) s[v].insert(p);
}
// cout << v << " :\n";
// for(auto x : s[v]) cout << x << " ";
// cout << "\n";
}
void solve() {
cin >> n;
if(n == 2) {
int x , y;
cin >> x >> y >> x >> y;
int q;
cin >> q;
while(q--) {
int z;
cin >> z;
if(z == 1) cout << min(x , y) << "\n";
else cout << "0\n";
}
return;
}
int f = 0;
for(int i = 1 ; i < n ; i++) {
int v , u , a , b;
cin >> v >> u >> a >> b; f += a + b;
g[v].pb({u , {a , b}});
g[u].pb({v , {b , a}});
}
// cout << "\n";
kfs(1 , -1);
lfs(1 , -1);
// for(int i = 1 ; i <= n ; i++)
// cout << i << " " << dw[i] << " " << up[i] << "\n";
efs(1 , -1);
ans[1] = dp[1];
rer(1 , -1);
ans[1] = ans[1];
ans[2] = ans[2];
dfs(root , -1);
// cout << root << "\n";
int q;
cin >> q;
int p = 3;
s[root].erase(--s[root].end());
while(s[root].size())
ans[p] = ans[p-1] + *(--s[root].end()) , p++ , s[root].erase(--s[root].end());
while(q--) {
int x;
cin >> x;
cout << f - ans[x] << "\n";
}
}
/*
4
1 2 1 2
1 3 3 4
1 4 5 6
2
1
2
5
1 2 1 2
2 3 2 3
3 4 3 4
4 5 4 5
5
1 2 3 4 5
*/
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
}
Compilation message
designated_cities.cpp: In function 'void lfs(long long int, long long int)':
designated_cities.cpp:74:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | if(p != suf[v].size() - 1) up[u] = max(up[u] , suf[v][p+1]);
| ~~^~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
39004 KB |
Output is correct |
2 |
Correct |
6 ms |
41288 KB |
Output is correct |
3 |
Correct |
6 ms |
41052 KB |
Output is correct |
4 |
Correct |
5 ms |
41052 KB |
Output is correct |
5 |
Correct |
5 ms |
41284 KB |
Output is correct |
6 |
Correct |
5 ms |
41288 KB |
Output is correct |
7 |
Correct |
5 ms |
41288 KB |
Output is correct |
8 |
Correct |
5 ms |
41232 KB |
Output is correct |
9 |
Correct |
6 ms |
41052 KB |
Output is correct |
10 |
Correct |
5 ms |
41052 KB |
Output is correct |
11 |
Correct |
5 ms |
41052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
39004 KB |
Output is correct |
2 |
Correct |
285 ms |
124372 KB |
Output is correct |
3 |
Correct |
334 ms |
123932 KB |
Output is correct |
4 |
Correct |
278 ms |
125008 KB |
Output is correct |
5 |
Correct |
264 ms |
101892 KB |
Output is correct |
6 |
Correct |
264 ms |
106596 KB |
Output is correct |
7 |
Correct |
244 ms |
94112 KB |
Output is correct |
8 |
Correct |
302 ms |
121940 KB |
Output is correct |
9 |
Correct |
176 ms |
83456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
39000 KB |
Output is correct |
2 |
Correct |
293 ms |
125012 KB |
Output is correct |
3 |
Correct |
351 ms |
123784 KB |
Output is correct |
4 |
Correct |
271 ms |
124240 KB |
Output is correct |
5 |
Correct |
267 ms |
102636 KB |
Output is correct |
6 |
Correct |
257 ms |
106064 KB |
Output is correct |
7 |
Correct |
179 ms |
87796 KB |
Output is correct |
8 |
Correct |
313 ms |
112684 KB |
Output is correct |
9 |
Correct |
175 ms |
82932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
39004 KB |
Output is correct |
2 |
Correct |
6 ms |
41288 KB |
Output is correct |
3 |
Correct |
6 ms |
41052 KB |
Output is correct |
4 |
Correct |
5 ms |
41052 KB |
Output is correct |
5 |
Correct |
5 ms |
41284 KB |
Output is correct |
6 |
Correct |
5 ms |
41288 KB |
Output is correct |
7 |
Correct |
5 ms |
41288 KB |
Output is correct |
8 |
Correct |
5 ms |
41232 KB |
Output is correct |
9 |
Correct |
6 ms |
41052 KB |
Output is correct |
10 |
Correct |
5 ms |
41052 KB |
Output is correct |
11 |
Correct |
5 ms |
41052 KB |
Output is correct |
12 |
Correct |
5 ms |
39000 KB |
Output is correct |
13 |
Correct |
7 ms |
41820 KB |
Output is correct |
14 |
Correct |
7 ms |
42072 KB |
Output is correct |
15 |
Correct |
8 ms |
41820 KB |
Output is correct |
16 |
Correct |
7 ms |
41820 KB |
Output is correct |
17 |
Correct |
9 ms |
41872 KB |
Output is correct |
18 |
Correct |
8 ms |
41972 KB |
Output is correct |
19 |
Correct |
8 ms |
41984 KB |
Output is correct |
20 |
Correct |
7 ms |
41820 KB |
Output is correct |
21 |
Correct |
7 ms |
41820 KB |
Output is correct |
22 |
Correct |
10 ms |
41808 KB |
Output is correct |
23 |
Correct |
8 ms |
41820 KB |
Output is correct |
24 |
Correct |
7 ms |
41636 KB |
Output is correct |
25 |
Correct |
7 ms |
41880 KB |
Output is correct |
26 |
Correct |
6 ms |
41564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
39004 KB |
Output is correct |
2 |
Correct |
285 ms |
124372 KB |
Output is correct |
3 |
Correct |
334 ms |
123932 KB |
Output is correct |
4 |
Correct |
278 ms |
125008 KB |
Output is correct |
5 |
Correct |
264 ms |
101892 KB |
Output is correct |
6 |
Correct |
264 ms |
106596 KB |
Output is correct |
7 |
Correct |
244 ms |
94112 KB |
Output is correct |
8 |
Correct |
302 ms |
121940 KB |
Output is correct |
9 |
Correct |
176 ms |
83456 KB |
Output is correct |
10 |
Correct |
6 ms |
39000 KB |
Output is correct |
11 |
Correct |
293 ms |
125012 KB |
Output is correct |
12 |
Correct |
351 ms |
123784 KB |
Output is correct |
13 |
Correct |
271 ms |
124240 KB |
Output is correct |
14 |
Correct |
267 ms |
102636 KB |
Output is correct |
15 |
Correct |
257 ms |
106064 KB |
Output is correct |
16 |
Correct |
179 ms |
87796 KB |
Output is correct |
17 |
Correct |
313 ms |
112684 KB |
Output is correct |
18 |
Correct |
175 ms |
82932 KB |
Output is correct |
19 |
Correct |
5 ms |
39000 KB |
Output is correct |
20 |
Correct |
313 ms |
124648 KB |
Output is correct |
21 |
Correct |
315 ms |
123732 KB |
Output is correct |
22 |
Correct |
281 ms |
124196 KB |
Output is correct |
23 |
Correct |
270 ms |
123728 KB |
Output is correct |
24 |
Correct |
282 ms |
125852 KB |
Output is correct |
25 |
Correct |
290 ms |
126076 KB |
Output is correct |
26 |
Correct |
307 ms |
123988 KB |
Output is correct |
27 |
Correct |
269 ms |
102408 KB |
Output is correct |
28 |
Correct |
257 ms |
106320 KB |
Output is correct |
29 |
Correct |
284 ms |
122960 KB |
Output is correct |
30 |
Correct |
292 ms |
119504 KB |
Output is correct |
31 |
Correct |
226 ms |
91388 KB |
Output is correct |
32 |
Correct |
291 ms |
113452 KB |
Output is correct |
33 |
Correct |
169 ms |
84416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
39004 KB |
Output is correct |
2 |
Correct |
6 ms |
41288 KB |
Output is correct |
3 |
Correct |
6 ms |
41052 KB |
Output is correct |
4 |
Correct |
5 ms |
41052 KB |
Output is correct |
5 |
Correct |
5 ms |
41284 KB |
Output is correct |
6 |
Correct |
5 ms |
41288 KB |
Output is correct |
7 |
Correct |
5 ms |
41288 KB |
Output is correct |
8 |
Correct |
5 ms |
41232 KB |
Output is correct |
9 |
Correct |
6 ms |
41052 KB |
Output is correct |
10 |
Correct |
5 ms |
41052 KB |
Output is correct |
11 |
Correct |
5 ms |
41052 KB |
Output is correct |
12 |
Correct |
5 ms |
39004 KB |
Output is correct |
13 |
Correct |
285 ms |
124372 KB |
Output is correct |
14 |
Correct |
334 ms |
123932 KB |
Output is correct |
15 |
Correct |
278 ms |
125008 KB |
Output is correct |
16 |
Correct |
264 ms |
101892 KB |
Output is correct |
17 |
Correct |
264 ms |
106596 KB |
Output is correct |
18 |
Correct |
244 ms |
94112 KB |
Output is correct |
19 |
Correct |
302 ms |
121940 KB |
Output is correct |
20 |
Correct |
176 ms |
83456 KB |
Output is correct |
21 |
Correct |
6 ms |
39000 KB |
Output is correct |
22 |
Correct |
293 ms |
125012 KB |
Output is correct |
23 |
Correct |
351 ms |
123784 KB |
Output is correct |
24 |
Correct |
271 ms |
124240 KB |
Output is correct |
25 |
Correct |
267 ms |
102636 KB |
Output is correct |
26 |
Correct |
257 ms |
106064 KB |
Output is correct |
27 |
Correct |
179 ms |
87796 KB |
Output is correct |
28 |
Correct |
313 ms |
112684 KB |
Output is correct |
29 |
Correct |
175 ms |
82932 KB |
Output is correct |
30 |
Correct |
5 ms |
39000 KB |
Output is correct |
31 |
Correct |
7 ms |
41820 KB |
Output is correct |
32 |
Correct |
7 ms |
42072 KB |
Output is correct |
33 |
Correct |
8 ms |
41820 KB |
Output is correct |
34 |
Correct |
7 ms |
41820 KB |
Output is correct |
35 |
Correct |
9 ms |
41872 KB |
Output is correct |
36 |
Correct |
8 ms |
41972 KB |
Output is correct |
37 |
Correct |
8 ms |
41984 KB |
Output is correct |
38 |
Correct |
7 ms |
41820 KB |
Output is correct |
39 |
Correct |
7 ms |
41820 KB |
Output is correct |
40 |
Correct |
10 ms |
41808 KB |
Output is correct |
41 |
Correct |
8 ms |
41820 KB |
Output is correct |
42 |
Correct |
7 ms |
41636 KB |
Output is correct |
43 |
Correct |
7 ms |
41880 KB |
Output is correct |
44 |
Correct |
6 ms |
41564 KB |
Output is correct |
45 |
Correct |
5 ms |
39000 KB |
Output is correct |
46 |
Correct |
313 ms |
124648 KB |
Output is correct |
47 |
Correct |
315 ms |
123732 KB |
Output is correct |
48 |
Correct |
281 ms |
124196 KB |
Output is correct |
49 |
Correct |
270 ms |
123728 KB |
Output is correct |
50 |
Correct |
282 ms |
125852 KB |
Output is correct |
51 |
Correct |
290 ms |
126076 KB |
Output is correct |
52 |
Correct |
307 ms |
123988 KB |
Output is correct |
53 |
Correct |
269 ms |
102408 KB |
Output is correct |
54 |
Correct |
257 ms |
106320 KB |
Output is correct |
55 |
Correct |
284 ms |
122960 KB |
Output is correct |
56 |
Correct |
292 ms |
119504 KB |
Output is correct |
57 |
Correct |
226 ms |
91388 KB |
Output is correct |
58 |
Correct |
291 ms |
113452 KB |
Output is correct |
59 |
Correct |
169 ms |
84416 KB |
Output is correct |
60 |
Correct |
5 ms |
39000 KB |
Output is correct |
61 |
Correct |
356 ms |
130376 KB |
Output is correct |
62 |
Correct |
340 ms |
125524 KB |
Output is correct |
63 |
Correct |
300 ms |
128340 KB |
Output is correct |
64 |
Correct |
308 ms |
130896 KB |
Output is correct |
65 |
Correct |
324 ms |
128340 KB |
Output is correct |
66 |
Correct |
318 ms |
129108 KB |
Output is correct |
67 |
Correct |
350 ms |
129104 KB |
Output is correct |
68 |
Correct |
305 ms |
106252 KB |
Output is correct |
69 |
Correct |
274 ms |
108992 KB |
Output is correct |
70 |
Correct |
299 ms |
125264 KB |
Output is correct |
71 |
Correct |
304 ms |
121224 KB |
Output is correct |
72 |
Correct |
245 ms |
96604 KB |
Output is correct |
73 |
Correct |
290 ms |
115536 KB |
Output is correct |
74 |
Correct |
196 ms |
89328 KB |
Output is correct |