제출 #1064196

#제출 시각아이디문제언어결과실행 시간메모리
1064196AmirAli_H1봉쇄 시간 (IOI23_closing)C++17
100 / 100
154 ms47304 KiB
// In the name of Allah

#include <bits/stdc++.h>
#include "closing.h"
using namespace std;

typedef			long long				ll;
typedef			pair<int, int>			pii;
typedef			pair<ll, ll>			pll;

#define			F						first
#define			S						second
#define			endl					'\n'
#define			sep						' '
#define			pb						push_back
#define			Mp						make_pair
#define			all(x)					(x).begin(),(x).end()
#define			len(x)					((ll) (x).size())

const int maxn = 4e5 + 7;

int n, u1, u2; ll R;
vector<pll> adj[maxn];
ll val[maxn], val1[maxn], val2[maxn];
ll sm1[maxn], sm2[maxn], mn1[maxn], mn2[maxn];
vector<ll> ls1; vector<pll> ls2;

void dfs(int v, int p = -1) {
	for (auto f : adj[v]) {
		int u = f.F; ll w = f.S;
		if (u == p) continue;
		val[u] = val[v] + w;
		dfs(u, v);
	}
}

int cal(ll R) {
	sort(all(ls1)); sort(all(ls2));
	
	for (int i = 0; i < len(ls1); i++) {
		if (i == 0) sm1[i] = ls1[i];
		else sm1[i] = sm1[i - 1] + ls1[i];
	}
	for (int i = 1; i <= len(ls2); i++) {
		sm2[i] = sm2[i - 1] + ls2[i - 1].F;
	}
	
	for (int i = 0; i < len(ls2); i++) {
		mn1[i] = ls2[i].S - ls2[i].F; mn2[i] = ls2[i].S;
	}
	for (int i = 1; i < len(ls2); i++) mn1[i] = min(mn1[i], mn1[i - 1]);
	for (int i = len(ls2) - 2; i >= 0; i--) mn2[i] = min(mn2[i], mn2[i + 1]);
	
	int ans = 0;
	for (int i = 0; i <= 2 * len(ls2); i++) {
		int x = i, j = i / 2;
		if (i % 2 == 0) {
			ll w = R - sm2[j];
			if (w >= 0) {
				x += upper_bound(sm1, sm1 + len(ls1), w) - sm1;
				ans = max(ans, x);
			}
		}
		else {
			ll wx = min(mn1[j] + ls2[j].F, mn2[j]);
			ll w = R - sm2[j] - wx;
			if (w >= 0) {
				x += upper_bound(sm1, sm1 + len(ls1), w) - sm1;
				ans = max(ans, x);
			}
		}
	}
	
	return ans;
}

int max_score(int N, int X, int Y, long long K, vector<int> U, vector<int> V, vector<int> W) {
	n = N; u1 = X; u2 = Y; R = K;
	for (int i = 0; i < n; i++) adj[i].clear();
	
	for (int i = 0; i < n - 1; i++) {
		int u = U[i], v = V[i]; ll w = W[i];
		adj[u].pb(Mp(v, w)); adj[v].pb(Mp(u, w));
	}
	
	val[u1] = 0; dfs(u1);
	for (int i = 0; i < n; i++) val1[i] = val[i];
	
	val[u2] = 0; dfs(u2);
	for (int i = 0; i < n; i++) val2[i] = val[i];
	
	for (int i = 0; i < n; i++) {
		if (2 * min(val1[i], val2[i]) <= max(val1[i], val2[i])) {
			ls1.pb(min(val1[i], val2[i]));
			ls1.pb(max(val1[i], val2[i]) - min(val1[i], val2[i]));
		}
		else {
			ls2.pb(Mp(max(val1[i], val2[i]), min(val1[i], val2[i])));
		}
	}
	
	int ans = 0;
	
	ls1.clear(); ls2.clear();
	for (int i = 0; i < n; i++) {
		ls1.pb(min(val1[i], val2[i]));
	}
	ans = max(ans, cal(R));
	
	ls1.clear(); ls2.clear();
	int x = 0; ll valx = R;
	for (int i = 0; i < n; i++) {
		if (val1[i] + val2[i] == val1[u2]) {
			valx -= min(val1[i], val2[i]); x++;
			ls1.pb(max(val1[i], val2[i]) - min(val1[i], val2[i]));
		}
		else {
			if (2 * min(val1[i], val2[i]) <= max(val1[i], val2[i])) {
				ls1.pb(min(val1[i], val2[i]));
				ls1.pb(max(val1[i], val2[i]) - min(val1[i], val2[i]));
			}
			else {
				ls2.pb(Mp(max(val1[i], val2[i]), min(val1[i], val2[i])));
			}
		}
	}
	if (valx >= 0) ans = max(ans, x + cal(valx));
	
	return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...