Submission #885526

# Submission time Handle Problem Language Result Execution time Memory
885526 2023-12-10T01:11:45 Z NK_ Designated Cities (JOI19_designated_cities) C++17
100 / 100
231 ms 47276 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define sz(x) int(x.size())

#define f first
#define s second
#define mp make_pair

#define pb push_back

using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;

template<class T> using V = vector<T>;
using vl = V<ll>;
using vpl = V<pl>;
using vpi = V<pi>;
using vi = V<int>;

using E = array<ll, 3>;

int main() {
	cin.tie(0)->sync_with_stdio(0);
		
	int N; cin >> N;

	V<V<E>> adj(N); vi deg(N); ll W = 0;
	for(int i = 0; i < N-1; i++) {
		int u, v, w, wi; cin >> u >> v >> w >> wi; --u, --v;
		adj[u].pb({v, w, wi});
		adj[v].pb({u, wi, w});
		deg[u]++, deg[v]++;
	}

	vl F(N); 
	function<void(int, int)> dfs = [&](int u, int p) {
		for(auto& e : adj[u]) {
			auto [v, tw, fw] = e; if (v == p) continue;
			W += tw; F[v] = F[u] + fw - tw;
			dfs(v, u);
		}
	};

	dfs(0, -1);	

	vl ans(N+1); ans[1] = W + *min_element(begin(F), end(F));

	vi dead(N); auto get = [&](int x) {
		for(auto& e : adj[x]) {
			if (!dead[e[0]]) return mp(e[2], e[0]);
		}

		return mp(-1LL, -1LL);
	};

	priority_queue<pl, vpl, greater<pl>> q; 
	for(int i = 0; i < N; i++) if (deg[i] == 1) {
		q.push(mp(get(i).f, i));
	}

	while(sz(q) > 2) {
		int u; ll w; tie(w, u) = q.top(); q.pop();
		// cout << u << " " << w << endl;

		dead[u] = 1;
		int p = get(u).s; deg[p]--;

		if (deg[p] == 1) q.push(mp(get(p).f + w, p));
		else {
			// cout << sz(q) << " " << w << endl;
			ans[sz(q)] = ans[sz(q) + 1] + w;
			// cout << ans[sz(q)] << " <= " << ans[sz(q) + 1] << endl;
		}
	}	

	int Q; cin >> Q;
	for(int i = 0; i < Q; i++) {
		int e; cin >> e;
		cout << ans[e] << nl;
	}

	exit(0-0);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 860 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 181 ms 25092 KB Output is correct
3 Correct 150 ms 38224 KB Output is correct
4 Correct 201 ms 24876 KB Output is correct
5 Correct 180 ms 25696 KB Output is correct
6 Correct 185 ms 27284 KB Output is correct
7 Correct 172 ms 25860 KB Output is correct
8 Correct 142 ms 38996 KB Output is correct
9 Correct 125 ms 27876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 185 ms 25076 KB Output is correct
3 Correct 147 ms 47188 KB Output is correct
4 Correct 190 ms 29904 KB Output is correct
5 Correct 171 ms 32004 KB Output is correct
6 Correct 186 ms 34128 KB Output is correct
7 Correct 145 ms 34556 KB Output is correct
8 Correct 150 ms 40464 KB Output is correct
9 Correct 140 ms 34048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 860 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 2 ms 724 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 2 ms 616 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 2 ms 688 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 2 ms 660 KB Output is correct
25 Correct 2 ms 856 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 181 ms 25092 KB Output is correct
3 Correct 150 ms 38224 KB Output is correct
4 Correct 201 ms 24876 KB Output is correct
5 Correct 180 ms 25696 KB Output is correct
6 Correct 185 ms 27284 KB Output is correct
7 Correct 172 ms 25860 KB Output is correct
8 Correct 142 ms 38996 KB Output is correct
9 Correct 125 ms 27876 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 185 ms 25076 KB Output is correct
12 Correct 147 ms 47188 KB Output is correct
13 Correct 190 ms 29904 KB Output is correct
14 Correct 171 ms 32004 KB Output is correct
15 Correct 186 ms 34128 KB Output is correct
16 Correct 145 ms 34556 KB Output is correct
17 Correct 150 ms 40464 KB Output is correct
18 Correct 140 ms 34048 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 190 ms 31372 KB Output is correct
21 Correct 153 ms 47276 KB Output is correct
22 Correct 184 ms 29932 KB Output is correct
23 Correct 186 ms 31804 KB Output is correct
24 Correct 204 ms 30408 KB Output is correct
25 Correct 201 ms 31740 KB Output is correct
26 Correct 186 ms 30404 KB Output is correct
27 Correct 178 ms 31492 KB Output is correct
28 Correct 189 ms 33736 KB Output is correct
29 Correct 183 ms 32092 KB Output is correct
30 Correct 178 ms 30984 KB Output is correct
31 Correct 151 ms 34648 KB Output is correct
32 Correct 159 ms 41164 KB Output is correct
33 Correct 129 ms 35836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 860 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 181 ms 25092 KB Output is correct
14 Correct 150 ms 38224 KB Output is correct
15 Correct 201 ms 24876 KB Output is correct
16 Correct 180 ms 25696 KB Output is correct
17 Correct 185 ms 27284 KB Output is correct
18 Correct 172 ms 25860 KB Output is correct
19 Correct 142 ms 38996 KB Output is correct
20 Correct 125 ms 27876 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 185 ms 25076 KB Output is correct
23 Correct 147 ms 47188 KB Output is correct
24 Correct 190 ms 29904 KB Output is correct
25 Correct 171 ms 32004 KB Output is correct
26 Correct 186 ms 34128 KB Output is correct
27 Correct 145 ms 34556 KB Output is correct
28 Correct 150 ms 40464 KB Output is correct
29 Correct 140 ms 34048 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 860 KB Output is correct
33 Correct 2 ms 724 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 2 ms 616 KB Output is correct
37 Correct 2 ms 604 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 2 ms 688 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 660 KB Output is correct
43 Correct 2 ms 856 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 190 ms 31372 KB Output is correct
47 Correct 153 ms 47276 KB Output is correct
48 Correct 184 ms 29932 KB Output is correct
49 Correct 186 ms 31804 KB Output is correct
50 Correct 204 ms 30408 KB Output is correct
51 Correct 201 ms 31740 KB Output is correct
52 Correct 186 ms 30404 KB Output is correct
53 Correct 178 ms 31492 KB Output is correct
54 Correct 189 ms 33736 KB Output is correct
55 Correct 183 ms 32092 KB Output is correct
56 Correct 178 ms 30984 KB Output is correct
57 Correct 151 ms 34648 KB Output is correct
58 Correct 159 ms 41164 KB Output is correct
59 Correct 129 ms 35836 KB Output is correct
60 Correct 0 ms 344 KB Output is correct
61 Correct 224 ms 33176 KB Output is correct
62 Correct 169 ms 46292 KB Output is correct
63 Correct 205 ms 32060 KB Output is correct
64 Correct 214 ms 33476 KB Output is correct
65 Correct 214 ms 32132 KB Output is correct
66 Correct 231 ms 33708 KB Output is correct
67 Correct 210 ms 32200 KB Output is correct
68 Correct 208 ms 33852 KB Output is correct
69 Correct 200 ms 35276 KB Output is correct
70 Correct 206 ms 33476 KB Output is correct
71 Correct 225 ms 33504 KB Output is correct
72 Correct 189 ms 35584 KB Output is correct
73 Correct 179 ms 41844 KB Output is correct
74 Correct 165 ms 38696 KB Output is correct