답안 #1065364

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1065364 2024-08-19T06:40:10 Z Gromp15 봉쇄 시간 (IOI23_closing) C++17
0 / 100
96 ms 53168 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;
const int is[]{0, 0, 1};

int max_score(int n, int X, int Y, long long K,
              std::vector<int> U, std::vector<int> V, std::vector<int> W) {
	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;
			for (int itr = 0; itr < 2 * N; itr++) {
				ll mn_cost = 1e18;
				int use = -1, z = -1;
				for (int v = 0; v < N; v++) {
					for (int l = 0; l < 2; l++) if (who[v] >> l & 1) {
						for (auto [u, w] : adj[v]) if (!(who[u] >> l & 1)) {
							if (ckmin(mn_cost, max(0LL, d[l][u] - dist[u]))) {
								use = u;
								z = l;
							}
						}
					}
				}
				if (used + mn_cost > K) break;
				used += mn_cost;
				who[use] |= 1 << z;
			}
			int ans2 = 0;
			for (int x : who) ans2 += (x & 1) + (x >> 1 & 1);
			ckmax(ans, ans2);
			/*
			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;
				}
			}
			for (int u = 0; u < n; u++) if (!inpath[u]) {
				if (allowed[u] == 3) {
					q2.insert({max(d[0][u], d[1][u]), u});
				}
				for (int i = 0; i < 2; i++) if (allowed[u] >> i & 1) {
					q.insert({d[i][u], u, i});
				}
			}
			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 == 2 && j== 1) {
					cout << "Q\n";
					for (auto x : q) cout << x[0] << " " << x[1] << " " << x[2] << '\n';
					cout << "Q2\n";
					for (auto x : q2) cout << x[0] << " " << x[1] << '\n';
				}
				if (q2.size() && (*q2.begin())[0] <= cost + nxt) {
					if (i == 2 && j == 1) cerr << "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) {
							assert(who[u] < 3);
							if (!who[u]) q2.insert({max(d[0][u], d[1][u]), u}), q.insert({d[z][u], u, z});
							else {
								int at = is[who[u]];
								assert(d[at][u] >= d[!at][u]);
								q.insert({d[at][u] - d[!at][u], u, at});
							}
						}
						else q.insert({d[z][u], u, z});
					}
					continue;
				}
after:;
				if (used + cost > K) break;
				if (i == 2 && j == 1) cerr << "USED " << v << " " << k << '\n';
				q.erase(q.begin());
				used += cost;
				who[v] |= 1 << k;
				if (allowed[v] == 3) {
					if (who[v] < 3) {
						assert(q2.count({max(d[0][v], d[1][v]), v}));
						q2.erase({max(d[0][v], d[1][v]), v});
					}
					if (who[v] == 3) {
						assert(!q.count({d[k][v], v, k}));
					}
					else {
						assert(q.count({d[!k][v], v, !k}));
						q.erase({d[!k][v], v, !k});
						assert(d[!k][v] >= d[k][v]);
						q.insert({d[!k][v] - d[k][v], v, !k});
					}
				}
				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) {
						assert(who[u] < 3);
						if (!who[u]) q2.insert({max(d[0][u], d[1][u]), u}), q.insert({d[z][u], u, z});
						else {
							int at = is[who[u]];
							assert(d[at][u] <= d[!at][u]);
							q.insert({d[!at][u] - d[at][u], u, !at});
						}
					}
					else q.insert({d[z][u], u, z});
				}
			}
			int cur = 0;
			for (int x : who) cout << x << " ";
			cout << '\n';
			for (int x : who) cur += __builtin_popcount(x);
			ckmax(ans, cur);
			*/
		}
	}
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 96 ms 53168 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '4'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '4'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '4'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '4'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '4'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '4'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '4'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '4'
4 Halted 0 ms 0 KB -