Submission #1117115

# Submission time Handle Problem Language Result Execution time Memory
1117115 2024-11-22T20:37:08 Z Nonoze Closing Time (IOI23_closing) C++17
0 / 100
1000 ms 87620 KB
#include <bits/stdc++.h>
using namespace std;

#ifndef _IN_LOCAL
	#define dbg(...)
#endif

#define endl '\n'
#define endlfl '\n' << flush
#define quit(x) return (void)(cout << x << endl)

template<typename T> void read(T& x) { cin >> x;}
template<typename T1, typename T2> void read(pair<T1, T2>& p) { read(p.first), read(p.second);}
template<typename T> void read(vector<T>& v) { for (auto& x : v) read(x); }
template<typename T1, typename T2> void read(T1& x, T2& y) { read(x), read(y); }
template<typename T1, typename T2, typename T3> void read(T1& x, T2& y, T3& z) { read(x), read(y), read(z); }
template<typename T1, typename T2, typename T3, typename T4> void read(T1& x, T2& y, T3& z, T4& zz) { read(x), read(y), read(z), read(zz); }
template<typename T> void print(vector<T>& v) { for (auto& x : v) cout << x << ' '; cout << endl; }

#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define fi first
#define se second
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define QYES quit("YES")
#define QNO quit("NO")

#define int long long
#define double long double
const int inf = numeric_limits<int>::max() / 4;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7, LOG=20;



int n, x, y, k;
vector<vector<pair<int, int>>> adj;
vector<int> c, p1, p2, dist1, dist2;

void dfsbase(int x, int t) {
	for (auto [u, v]: adj[x]) if ((t==1 && u!=p1[x]) || (t==2 && u!=p2[x])) {
		if (t==1) p1[u]=x;
		else p2[u]=x;
		dfsbase(u, t);
	}
}

vector<vector<vector<pair<int, int>>>> ansi;

vector<pair<int, int>> dfs(int u, int only=-1) {
	if (sz(ansi[u][only+1])) return ansi[u][only+1];
	if (u==x || u==y) return ansi[u][only+1]={{0, 0}, {0, 0}, {0, 0}};
	vector<pair<int, int>> ans(3);
	if (p1[u]==p2[u]) {
		ans=dfs(p1[u]);
		int cnt=0;
		if (c[u]<dist1[u]) ans[0].fi+=dist1[u]-c[u], ans[0].se++, cnt++;
		if (c[u]<dist2[u]) ans[1].fi+=dist2[u]-c[u], ans[1].se++, cnt++;
		if (c[u]<max(dist1[u], dist2[u])) ans[2].fi+=max(dist1[u], dist2[u])-c[u], ans[2].se+=cnt;
		return ansi[u][only+1]=ans;
	}
	if (only==-1) {
		ans=dfs(p1[u], 0);
		auto temp=dfs(p2[u], 1); ans[1]=temp[1];
		ans[2]={ans[0].fi+ans[1].fi, ans[0].se+ans[1].se};
		
		int cnt=0;
		if (c[u]<dist1[u]) ans[0].fi+=dist1[u]-c[u], ans[0].se++, cnt++;
		if (c[u]<dist2[u]) ans[1].fi+=dist2[u]-c[u], ans[1].se++, cnt++;
		if (c[u]<max(dist1[u], dist2[u])) ans[2].fi+=max(dist1[u], dist2[u])-c[u], ans[2].se+=cnt;

		return ansi[u][only+1]=ans;
	} else if (only==0) {
		ans=dfs(p1[u], only);
		if (c[u]<dist1[u]) ans[0].fi+=dist1[u]-c[u], ans[0].se++;
		return ansi[u][only+1]=ans;
	} else {
		ans=dfs(p2[u], only);
		if (c[u]<dist2[u]) ans[1].fi+=dist2[u]-c[u], ans[1].se++;
		return ansi[u][only+1]=ans;
	}
	return ansi[u][only+1]=ans;
}


void update(int u, int nb) {
	if (u==x || u==y) return;
	if (nb==2) {
		k-=max(max(dist1[u], dist2[u])-c[u], 0LL);
		c[u]=max(dist1[u], dist2[u]);
		if (p1[u]==p2[u]) {
			update(p1[u], nb);
			return;
		}
		update(p1[u], 0);
		update(p2[u], 1);
	} else if (nb==0) {
		k-=max(dist1[u]-c[u], 0LL);
		cmax(c[u], dist1[u]);
		update(p1[u], nb);
	} else {
		k-=max(dist2[u]-c[u], 0LL);
		cmax(c[u], dist2[u]);
		update(p2[u], nb);
	}
}


signed max_score(signed N, signed X, signed Y, long long K, vector<signed> U, vector<signed> V, vector<signed> W) {
	n = N, x = X, y = Y, k = K, adj.clear(), adj.resize(n);
	for (int i=0; i<n-1; i++) {
		adj[U[i]].pb(mp(V[i], W[i]));
		adj[V[i]].pb(mp(U[i], W[i]));
	}

	dist1.clear(), dist2.clear(), dist1.resize(n, inf), dist2.resize(n, inf);
	{
		queue<pair<int, int>> q; q.push({x, 0});
		dist1[x] = 0;
		while (!q.empty()) {
			auto [u, d] = q.front(); q.pop();
			for (auto& [v, w] : adj[u]) {
				if (dist1[v] == inf) {
					dist1[v] = dist1[u] + w;
					q.push({v, d+w});
				}
			}
		}
	}
	{
		queue<pair<int, int>> q; q.push({y, 0});
		dist2[y] = 0;
		while (!q.empty()) {
			auto [u, d] = q.front(); q.pop();
			for (auto& [v, w] : adj[u]) {
				if (dist2[v] == inf) {
					dist2[v] = dist2[u] + w;
					q.push({v, d+w});
				}
			}
		}
	}


	c.clear(), c.resize(n, 0), p1.clear(), p1.resize(n, -1), p2.clear(), p2.resize(n, -1);
	dfsbase(x, 1), dfsbase(y, 2);
	int ans=2;
	for (int t=0; t<n; t++) {
		ansi.clear(), ansi.resize(n, vector<vector<pair<int, int>>>(3));
		pair<int, int> best={-1, -1};
		double actbest=inf;
		for (int i=0; i<n; i++) {
			dfs(i);
			for (int j=0; j<3; j++) {
				auto u=ansi[i][0][j];
				if (u.fi<=k && u.se) {
					double act=1.0*u.fi/u.se;
					if (act<actbest) {
						actbest=act;
						best={i, j};
					}
				}
			}
		}
		if (best.fi==-1) break;
		int i=best.fi, j=best.se;
		auto u=ansi[i][0][j];
		ans+=u.se;
		update(i, j);
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1067 ms 87620 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 504 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Incorrect 1 ms 336 KB 1st lines differ - on the 1st token, expected: '34', found: '27'
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 504 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Incorrect 1 ms 336 KB 1st lines differ - on the 1st token, expected: '34', found: '27'
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 504 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Incorrect 1 ms 336 KB 1st lines differ - on the 1st token, expected: '34', found: '27'
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Incorrect 1 ms 336 KB 1st lines differ - on the 1st token, expected: '34', found: '27'
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Incorrect 1 ms 336 KB 1st lines differ - on the 1st token, expected: '34', found: '27'
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Incorrect 1 ms 336 KB 1st lines differ - on the 1st token, expected: '34', found: '27'
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Incorrect 1 ms 336 KB 1st lines differ - on the 1st token, expected: '34', found: '27'
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Incorrect 1 ms 336 KB 1st lines differ - on the 1st token, expected: '34', found: '27'
6 Halted 0 ms 0 KB -