Submission #1065267

# Submission time Handle Problem Language Result Execution time Memory
1065267 2024-08-19T05:32:53 Z Gromp15 Closing Time (IOI23_closing) C++17
9 / 100
72 ms 24528 KB
#include <bits/stdc++.h>
#include "closing.h"
#define ar array
#define ll long long
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

const ll INF = 1e18;

int max_score(int n, int X, int Y, long long K,
              std::vector<int> U, std::vector<int> V, std::vector<int> W) {
		int N = n;
	vector<vector<ar<int, 2>>> adj(n+1);
	for (int i = 0; i < n-1; i++) {
		adj[U[i]].push_back({V[i], W[i]});
		adj[V[i]].push_back({U[i], W[i]});
	}
	vector<vector<ll>> d(2, vector<ll>(n, INF));
	vector<int> p(n, -1);
	auto dfs = [&](auto&& s, int v, vector<ll>& dist) -> void {
		for (auto [u, w] : adj[v]) if (u != p[v]) {
			p[u] = v, dist[u] = dist[v] + w;
			s(s, u, dist);
		}
	};
	d[0][X] = 0, d[1][Y] = 0;
	dfs(dfs, X, d[0]);
	fill(all(p), -1);
	dfs(dfs, Y, d[1]);
	vector<int> pth;
	for (int x = X; x != Y; x = p[x]) pth.emplace_back(x);
	pth.emplace_back(Y);
	vector<bool> vis(1 << N);
	int ans = 0;
	for (int mask = 0; mask < 1 << N; mask++) if (mask >> Y & 1) {
		int real_mask = mask;
		for (int i = 0; i < N; i++) if (mask >> i & 1) {
			int tmp = i;
			while (tmp != Y) real_mask |= 1 << tmp, tmp = p[tmp];
		}
		if (vis[real_mask]) continue;
		vis[real_mask] = 1;
		vector<ll> dist(N);
		ll u = 0;
		for (int i = 0; i < N; i++) if (real_mask >> i & 1) dist[i] = d[1][i], u += d[1][i];
		if (u > K) continue;
		vector<bool> vis2(N);
		vis2[X] = 1;
		for (int itr = 0; itr < N - 1; itr++) {
			ll mn_cost = 1e18; int who = -1;
			for (int i = 0; i < N; i++) if (vis2[i]) {
				for (auto [u, w] : adj[i]) if (!vis2[u]) {
					if (ckmin(mn_cost, max(0LL, d[0][u] - dist[u]))) who = u;
				}
			}
			if (u + mn_cost > K) break;
			u += mn_cost;
			assert(~who);
			vis2[who] = 1;
		}
		int cur = __builtin_popcount(real_mask);
		for (int i = 0; i < N; i++) cur += vis2[i];
		if (ckmax(ans, cur)) {
			cerr << "Ans\n";
			for (int i = 0; i < N; i++) cerr << (real_mask >> i & 1) << " \n"[i==N-1];
			for (int i = 0; i < N; i++) cerr << vis2[i] << " \n"[i==N-1];
		}
	}
	return ans;
	/*
	vector<vector<ar<int, 2>>> adj(n+1);
	for (int i = 0; i < n-1; i++) {
		adj[U[i]].push_back({V[i], W[i]});
		adj[V[i]].push_back({U[i], W[i]});
	}
	vector<vector<ll>> d(2, vector<ll>(n, INF));
	vector<int> p(n, -1);
	auto dfs = [&](auto&& s, int v, vector<ll>& dist) -> void {
		for (auto [u, w] : adj[v]) if (u != p[v]) {
			p[u] = v, dist[u] = dist[v] + w;
			s(s, u, dist);
		}
	};
	d[0][X] = 0, d[1][Y] = 0;
	dfs(dfs, X, d[0]);
	fill(all(p), -1);
	dfs(dfs, Y, d[1]);
	vector<int> pth;
	for (int x = X; x != Y; x = p[x]) pth.emplace_back(x);
	pth.emplace_back(Y);
	const int N = sz(pth);
	vector<bool> inpath(N);
	for (int x : pth) inpath[x] = 1;
	int ans = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			vector<ll> dist(n);
			vector<int> who(n), allowed(n);
			for (int k = 0; k <= i; k++) { 
				ckmax(dist[pth[k]], d[0][pth[k]]);
				who[pth[k]] |= 1;
			}
			for (int k = j; k < N; k++) { 
				ckmax(dist[pth[k]], d[1][pth[k]]);
				who[pth[k]] |= 2;
			}
			ll used = 0;
			for (int v : pth) { 
				used += dist[v];
				if (used > K) break;
			}
			if (used > K) continue;
			set<ar<ll, 3>> q;
			set<ar<ll, 2>> q2;
			for (int v : pth) for (int z = 0; z < 2; z++) if (who[v] >> z & 1) {
				for (auto [u, w] : adj[v]) if (!inpath[u] && !(allowed[u] >> z & 1)) {
					allowed[u] |= 1 << z;
					if (allowed[u] == 3) q2.insert({max(d[0][u], d[1][u]), u});
					q.insert({d[z][u], u, z});
				}
			}
			while (q.size()) {
				auto [cost, v, k] = *q.begin();
				assert(!(who[v] >> k & 1));
				ll nxt = q.size() > 1 ? (*next(q.begin()))[0] : INF;
				if (i == 1 && j == 0) { 
					//cout << "V " << v << " " << cost << " " << k << " " << nxt << " " << (q2.size() ? (*q2.begin())[0] : INF) << '\n';
				}
				if (q2.size() && (*q2.begin())[0] <= cost + nxt) {
					//if (i == 1 && j == 0) cout << "HI " << (*q2.begin())[0] << " " << (*q2.begin())[1] << '\n';
					auto [cost2, v2] = *q2.begin();
					if (used + cost2 > K) {
						goto after;
					}
					q2.erase(q2.begin());
					used += cost2;
					assert(!who[v2]);
					assert(allowed[v2] == 3);
					for (int i = 0; i < 2; i++) {
						assert(q.count({d[i][v2], v2, i}));
						q.erase({d[i][v2], v2, i});
					}
					who[v2] = 3;
					for (auto [u, w] : adj[v2]) for (int z = 0; z < 2; z++) if (!inpath[u] && !(allowed[u] >> z & 1)) {
						allowed[u] |= 1 << z;
						if (allowed[u] == 3 && !who[u]) {
							q2.insert({max(d[0][u], d[1][u]), u});
						}
						q.insert({d[z][u], u, z});
					}
					continue;
				}
after:;
				if (used + cost > K) break;
				//if (i == 1 && j == 0) cout << "USED " << v << '\n';
				q.erase(q.begin());
				used += cost;
				who[v] |= 1 << k;
				if (allowed[v] == 3 && q2.count({max(d[0][v], d[1][v]), v})) q2.erase({max(d[0][v], d[1][v]), v});
				assert(!inpath[v]);
				for (auto [u, w] : adj[v]) for (int z = 0; z < 2; z++) if ((who[v] >> z & 1) && !inpath[u] && !(allowed[u] >> z & 1)) {
					allowed[u] |= 1 << z;
					if (allowed[u] == 3 && !who[u]) q2.insert({max(d[0][u], d[1][u]), u});
					q.insert({d[z][u], u, z});
				}
			}
			int cur = 0;
			for (int x : who) cur += __builtin_popcount(x);
			ckmax(ans, cur);
		}
	}
	return ans;
	*/
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 72 ms 24528 KB 1st lines differ - on the 1st token, expected: '451', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 47 ms 348 KB Output is correct
3 Correct 63 ms 348 KB Output is correct
4 Correct 66 ms 348 KB Output is correct
5 Correct 27 ms 348 KB Output is correct
6 Incorrect 1 ms 344 KB 1st lines differ - on the 1st token, expected: '96', found: '0'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 47 ms 348 KB Output is correct
3 Correct 63 ms 348 KB Output is correct
4 Correct 66 ms 348 KB Output is correct
5 Correct 27 ms 348 KB Output is correct
6 Incorrect 1 ms 344 KB 1st lines differ - on the 1st token, expected: '96', found: '0'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 47 ms 348 KB Output is correct
3 Correct 63 ms 348 KB Output is correct
4 Correct 66 ms 348 KB Output is correct
5 Correct 27 ms 348 KB Output is correct
6 Incorrect 1 ms 344 KB 1st lines differ - on the 1st token, expected: '96', found: '0'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 47 ms 348 KB Output is correct
4 Correct 63 ms 348 KB Output is correct
5 Correct 66 ms 348 KB Output is correct
6 Correct 27 ms 348 KB Output is correct
7 Correct 1 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 10 ms 348 KB Output is correct
11 Correct 21 ms 344 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 42 ms 348 KB Output is correct
14 Correct 23 ms 504 KB Output is correct
15 Correct 12 ms 348 KB Output is correct
16 Correct 11 ms 472 KB Output is correct
17 Correct 10 ms 476 KB Output is correct
18 Correct 22 ms 348 KB Output is correct
19 Correct 44 ms 344 KB Output is correct
20 Correct 12 ms 348 KB Output is correct
21 Correct 11 ms 348 KB Output is correct
22 Correct 43 ms 348 KB Output is correct
23 Correct 49 ms 348 KB Output is correct
24 Correct 12 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 47 ms 348 KB Output is correct
4 Correct 63 ms 348 KB Output is correct
5 Correct 66 ms 348 KB Output is correct
6 Correct 27 ms 348 KB Output is correct
7 Incorrect 1 ms 344 KB 1st lines differ - on the 1st token, expected: '96', found: '0'
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 47 ms 348 KB Output is correct
4 Correct 63 ms 348 KB Output is correct
5 Correct 66 ms 348 KB Output is correct
6 Correct 27 ms 348 KB Output is correct
7 Incorrect 1 ms 344 KB 1st lines differ - on the 1st token, expected: '96', found: '0'
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 47 ms 348 KB Output is correct
4 Correct 63 ms 348 KB Output is correct
5 Correct 66 ms 348 KB Output is correct
6 Correct 27 ms 348 KB Output is correct
7 Incorrect 1 ms 344 KB 1st lines differ - on the 1st token, expected: '96', found: '0'
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 47 ms 348 KB Output is correct
4 Correct 63 ms 348 KB Output is correct
5 Correct 66 ms 348 KB Output is correct
6 Correct 27 ms 348 KB Output is correct
7 Incorrect 1 ms 344 KB 1st lines differ - on the 1st token, expected: '96', found: '0'
8 Halted 0 ms 0 KB -