/*
I find it wholesome to be alone the greater part of the time.
To be in company, even with the best, is soon wearisome and dissipating.
I love to be alone.
I never found the companion that was so companionable as solitude.
*/
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
#define int long long
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 3e5 + 1;
//const int Mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int n;
struct TEdge{
int v, w, rw;
};
vector <TEdge> adj[maxN];
ll sum = 0;
ll ans[maxN];
int deg[maxN];
void dfs(int u, int p){
for (auto &[v, w, rw]: adj[u]){
if (v == p) continue;
sum += w;
dfs(v, u);
}
}
void dfs1(int u, int p){
ans[1] = min(ans[1], sum);
for (auto &[v, w, rw]: adj[u]){
if (v == p) continue;
sum -= w - rw;
dfs1(v, u);
sum += w - rw;
}
}
bool visited[maxN];
pii find_nxt(int u){
for (auto &[v, w, rw]: adj[u]){
if (!visited[v]){
return {rw, v};
}
}
}
void Init(){
cin >> n;
for (int i = 1; i < n; ++i){
int u, v, w, rw;
cin >> u >> v >> w >> rw;
adj[u].pb({v, w, rw});
adj[v].pb({u, rw, w});
++deg[u];
++deg[v];
}
ans[1] = inf;
dfs(1, 0);
dfs1(1, 0);
priority_queue <pii, vector <pii>, greater <pii>> PQ;
for (int i = 1; i <= n; ++i){
if (deg[i] == 1){
PQ.push({find_nxt(i).fi, i});
}
}
while (PQ.size() > 2){
auto r = PQ.top(); PQ.pop();
visited[r.se] = 1;
auto p = find_nxt(r.se);
if (--deg[p.se] == 1){
PQ.push(pii{find_nxt(p.se).fi + r.fi, p.se});
}
else{
ans[PQ.size()] = ans[PQ.size() + 1] + r.fi;
}
}
int q; cin >> q;
while (q--){
int x; cin >> x; cout << ans[x] << "\n";
}
}
#define debug
#define taskname "test"
signed main(){
faster
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
int tt = 1;
//cin >> tt;
while (tt--){
Init();
}
if (fopen("timeout.txt", "r")){
ofstream timeout("timeout.txt");
timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
timeout.close();
#ifndef debug
cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
#endif // debug
}
}
Compilation message
designated_cities.cpp: In function 'pii find_nxt(long long int)':
designated_cities.cpp:56:1: warning: control reaches end of non-void function [-Wreturn-type]
56 | }
| ^
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7264 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
308 ms |
30536 KB |
Output is correct |
3 |
Correct |
277 ms |
36076 KB |
Output is correct |
4 |
Correct |
285 ms |
29236 KB |
Output is correct |
5 |
Correct |
295 ms |
31616 KB |
Output is correct |
6 |
Correct |
292 ms |
31944 KB |
Output is correct |
7 |
Correct |
261 ms |
32496 KB |
Output is correct |
8 |
Correct |
248 ms |
37032 KB |
Output is correct |
9 |
Correct |
202 ms |
35300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
307 ms |
30544 KB |
Output is correct |
3 |
Correct |
238 ms |
37388 KB |
Output is correct |
4 |
Correct |
267 ms |
29252 KB |
Output is correct |
5 |
Correct |
295 ms |
31600 KB |
Output is correct |
6 |
Correct |
296 ms |
32064 KB |
Output is correct |
7 |
Correct |
223 ms |
34884 KB |
Output is correct |
8 |
Correct |
260 ms |
35424 KB |
Output is correct |
9 |
Correct |
190 ms |
34932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7264 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
5 ms |
7508 KB |
Output is correct |
14 |
Correct |
5 ms |
7508 KB |
Output is correct |
15 |
Correct |
5 ms |
7484 KB |
Output is correct |
16 |
Correct |
5 ms |
7508 KB |
Output is correct |
17 |
Correct |
5 ms |
7600 KB |
Output is correct |
18 |
Correct |
6 ms |
7508 KB |
Output is correct |
19 |
Correct |
5 ms |
7508 KB |
Output is correct |
20 |
Correct |
6 ms |
7508 KB |
Output is correct |
21 |
Correct |
5 ms |
7508 KB |
Output is correct |
22 |
Correct |
5 ms |
7508 KB |
Output is correct |
23 |
Correct |
5 ms |
7508 KB |
Output is correct |
24 |
Correct |
5 ms |
7508 KB |
Output is correct |
25 |
Correct |
5 ms |
7636 KB |
Output is correct |
26 |
Correct |
5 ms |
7508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
308 ms |
30536 KB |
Output is correct |
3 |
Correct |
277 ms |
36076 KB |
Output is correct |
4 |
Correct |
285 ms |
29236 KB |
Output is correct |
5 |
Correct |
295 ms |
31616 KB |
Output is correct |
6 |
Correct |
292 ms |
31944 KB |
Output is correct |
7 |
Correct |
261 ms |
32496 KB |
Output is correct |
8 |
Correct |
248 ms |
37032 KB |
Output is correct |
9 |
Correct |
202 ms |
35300 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
307 ms |
30544 KB |
Output is correct |
12 |
Correct |
238 ms |
37388 KB |
Output is correct |
13 |
Correct |
267 ms |
29252 KB |
Output is correct |
14 |
Correct |
295 ms |
31600 KB |
Output is correct |
15 |
Correct |
296 ms |
32064 KB |
Output is correct |
16 |
Correct |
223 ms |
34884 KB |
Output is correct |
17 |
Correct |
260 ms |
35424 KB |
Output is correct |
18 |
Correct |
190 ms |
34932 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
306 ms |
30588 KB |
Output is correct |
21 |
Correct |
233 ms |
37472 KB |
Output is correct |
22 |
Correct |
263 ms |
29280 KB |
Output is correct |
23 |
Correct |
283 ms |
30884 KB |
Output is correct |
24 |
Correct |
281 ms |
29764 KB |
Output is correct |
25 |
Correct |
291 ms |
30964 KB |
Output is correct |
26 |
Correct |
279 ms |
29748 KB |
Output is correct |
27 |
Correct |
276 ms |
31228 KB |
Output is correct |
28 |
Correct |
299 ms |
31884 KB |
Output is correct |
29 |
Correct |
275 ms |
31048 KB |
Output is correct |
30 |
Correct |
267 ms |
30820 KB |
Output is correct |
31 |
Correct |
232 ms |
33160 KB |
Output is correct |
32 |
Correct |
259 ms |
35680 KB |
Output is correct |
33 |
Correct |
193 ms |
35304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7264 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
5 ms |
7380 KB |
Output is correct |
13 |
Correct |
308 ms |
30536 KB |
Output is correct |
14 |
Correct |
277 ms |
36076 KB |
Output is correct |
15 |
Correct |
285 ms |
29236 KB |
Output is correct |
16 |
Correct |
295 ms |
31616 KB |
Output is correct |
17 |
Correct |
292 ms |
31944 KB |
Output is correct |
18 |
Correct |
261 ms |
32496 KB |
Output is correct |
19 |
Correct |
248 ms |
37032 KB |
Output is correct |
20 |
Correct |
202 ms |
35300 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
22 |
Correct |
307 ms |
30544 KB |
Output is correct |
23 |
Correct |
238 ms |
37388 KB |
Output is correct |
24 |
Correct |
267 ms |
29252 KB |
Output is correct |
25 |
Correct |
295 ms |
31600 KB |
Output is correct |
26 |
Correct |
296 ms |
32064 KB |
Output is correct |
27 |
Correct |
223 ms |
34884 KB |
Output is correct |
28 |
Correct |
260 ms |
35424 KB |
Output is correct |
29 |
Correct |
190 ms |
34932 KB |
Output is correct |
30 |
Correct |
4 ms |
7380 KB |
Output is correct |
31 |
Correct |
5 ms |
7508 KB |
Output is correct |
32 |
Correct |
5 ms |
7508 KB |
Output is correct |
33 |
Correct |
5 ms |
7484 KB |
Output is correct |
34 |
Correct |
5 ms |
7508 KB |
Output is correct |
35 |
Correct |
5 ms |
7600 KB |
Output is correct |
36 |
Correct |
6 ms |
7508 KB |
Output is correct |
37 |
Correct |
5 ms |
7508 KB |
Output is correct |
38 |
Correct |
6 ms |
7508 KB |
Output is correct |
39 |
Correct |
5 ms |
7508 KB |
Output is correct |
40 |
Correct |
5 ms |
7508 KB |
Output is correct |
41 |
Correct |
5 ms |
7508 KB |
Output is correct |
42 |
Correct |
5 ms |
7508 KB |
Output is correct |
43 |
Correct |
5 ms |
7636 KB |
Output is correct |
44 |
Correct |
5 ms |
7508 KB |
Output is correct |
45 |
Correct |
4 ms |
7380 KB |
Output is correct |
46 |
Correct |
306 ms |
30588 KB |
Output is correct |
47 |
Correct |
233 ms |
37472 KB |
Output is correct |
48 |
Correct |
263 ms |
29280 KB |
Output is correct |
49 |
Correct |
283 ms |
30884 KB |
Output is correct |
50 |
Correct |
281 ms |
29764 KB |
Output is correct |
51 |
Correct |
291 ms |
30964 KB |
Output is correct |
52 |
Correct |
279 ms |
29748 KB |
Output is correct |
53 |
Correct |
276 ms |
31228 KB |
Output is correct |
54 |
Correct |
299 ms |
31884 KB |
Output is correct |
55 |
Correct |
275 ms |
31048 KB |
Output is correct |
56 |
Correct |
267 ms |
30820 KB |
Output is correct |
57 |
Correct |
232 ms |
33160 KB |
Output is correct |
58 |
Correct |
259 ms |
35680 KB |
Output is correct |
59 |
Correct |
193 ms |
35304 KB |
Output is correct |
60 |
Correct |
4 ms |
7380 KB |
Output is correct |
61 |
Correct |
291 ms |
33300 KB |
Output is correct |
62 |
Correct |
264 ms |
37828 KB |
Output is correct |
63 |
Correct |
289 ms |
31940 KB |
Output is correct |
64 |
Correct |
323 ms |
33708 KB |
Output is correct |
65 |
Correct |
311 ms |
32108 KB |
Output is correct |
66 |
Correct |
326 ms |
33684 KB |
Output is correct |
67 |
Correct |
300 ms |
32192 KB |
Output is correct |
68 |
Correct |
302 ms |
33992 KB |
Output is correct |
69 |
Correct |
316 ms |
34456 KB |
Output is correct |
70 |
Correct |
306 ms |
33332 KB |
Output is correct |
71 |
Correct |
294 ms |
33388 KB |
Output is correct |
72 |
Correct |
276 ms |
35912 KB |
Output is correct |
73 |
Correct |
285 ms |
37308 KB |
Output is correct |
74 |
Correct |
225 ms |
39528 KB |
Output is correct |