제출 #1345638

#제출 시각아이디문제언어결과실행 시간메모리
1345638limitsSplit the Attractions (IOI19_split)C++20
0 / 100
536 ms1114112 KiB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3,unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define f0r(i, n) for (auto i = 0; i < (n); ++i)
#define fnr(i, n, k) for (auto i = (n); i < (k); ++i)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define F first
#define S second
#define ctn(x) cout << x << '\n'
#define forl(a, l) for (auto a : l)
#define ctl(l) for (auto &a : (l)) cout << a << ' '; cout << endl;
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define ub(v, x) (upper_bound(all(v), x) - begin(v))
#define pq priority_queue

template <class T>
using V = vector<T>;
using ll = long long;
using vi = V<int>;
using vl = V<ll>;
using pi = pair<int, int>;
using ti = tuple<int, int, int>;
using Adj = V<vi>;
using vvi = V<vi>;

#include "split.h"

using namespace std;

Adj G;
V<bool> vis;
vi res, sz, par;
void dfs(int u, int &cnt, int b) {
	if (cnt >= b) return;
	res[u] = 2;
	vis[u] = 1;
	if (++cnt == b) return;
	forl(c, G[u]) if (!vis[c]) {
		dfs(c, cnt, b);
	}
}

V<bool> rem;
void dfs(int v) {
	if (rem[v]) return;
	sz[v] = 1;
	forl(c, G[v]) if (c != par[v]) {
		par[c] = v;
		dfs(c);
		sz[v] += c;
	}
}

void get_res(int v, vi &out, int b) {
	if (out.size() == b) return;
	out.pb(v);
	rem[v] = 1;
	forl(c, G[v]) if (c != par[v]) get_res(c, out, b);
}

vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q) {
	G.assign(n, {});
	vis.assign(n, 0);
	res.assign(n, 0);
	par.assign(n, 0);
	sz.assign(n, 0);
	rem.assign(n, 0);

	int m = p.size();
	f0r(i, m) {
		G[p[i]].pb(q[i]);
		G[q[i]].pb(p[i]);
	}

	V<pi> lab{{a, 1}, {b, 2}, {c, 3}};
	sort(all(lab));

	dfs(0);
	V<pi> ps;
	f0r(i, n) ps.pb({sz[i], i});
	sort(all(ps));
	auto it = lower_bound(all(ps), make_pair(lab[0].F, 0));
	if (it == ps.end()) return vi(n);
	vi out;
	get_res(it->S, out, lab[0].F);
	forl(x, out) res[x] = lab[0].S;

	sz.assign(n, 0);
	dfs(0);
	ps.clear();
	f0r(i, n) ps.pb({sz[i], i});
	sort(all(ps));
	it = lower_bound(all(ps), make_pair(lab[1].F, 0));
	if (it == ps.end()) return vi(n);
	out.clear();
	get_res(it->S, out, lab[1].F);
	forl(x, out) res[x] = lab[1].S;

	f0r(i, n) if (!res[i]) res[i] = lab[2].S;
	return res;

	if (a == 1) {
		int cnt = 0;
		dfs(0, cnt, b);
		bool f = 0;
		f0r(i, n) if (!vis[i]) {
			if (!f) {
				f=1;
				res[i] = 1;
			} else res[i] = 3;
		}
		return res;
	}
	return res;
}
#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...