This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
#include "split.h"
using namespace std;
#define F first
#define S second
#define rep(i, n) for (int i = 1; i <= n; i++)
#define all(x) (x).begin(), (x).end()
#define comp(x) sort(all((x))); (x).erase(unique(all((x))), (x).end())
#define sz(x) (int)((x).size())
#define sq(x) (x) * (x)
#define srt(x) sort(all(x))
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
#define yes cout << "Yes\n"
#define no cout << "No\n"
#define imp cout << "-1\n"
#define el cout << "\n"
const int MAX = 1e5 + 5;
const int LOG = 20;
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
const int dy[8] = { -1, 0, 1, 0, -1, 1, 1, -1 };
const int dx[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };
template<typename ...Args>
void read(Args&... args) { (cin >> ... >> args); }
class DisjointSet {
public:
DisjointSet(int n): n(n), par(n + 5), s(n + 5) {
init();
}
int n; vector<int> par, s;
void init(void)
{
iota(all(par), 0);
fill(all(s), 1);
}
int Find(int x)
{
if (x == par[x]) return x;
return par[x] = Find(par[x]);
}
bool Union(int p, int q)
{
p = Find(p); q = Find(q); if (p == q) return false;
par[q] = p; s[p] += s[q]; s[q] = 0; return true;
}
bool same(int p, int q)
{
p = Find(p); q = Find(q);
return (p == q);
}
};
vi vc[MAX], szz(MAX);
vector<bool> vst(MAX);
int getSize(int node, int par)
{
szz[node] = 1;
for (auto &each : vc[node])
{
if (each == par) continue;
szz[node] += getSize(each, node);
}
return szz[node];
}
int getCent(int node, int par, int half)
{
for (auto &each : vc[node])
{
if (each == par) continue;
if (szz[each] > half) return getCent(each, node, half);
}
return node;
}
vi path;
int getComp(int node, int par)
{
int cnt = 1;
path.pb(node);
for (auto &each : vc[node])
{
if (each == par) continue;
cnt += getComp(each, node);
}
return cnt;
}
vector<int> find_split(int N, int A, int B, int C, vector<int> P, vector<int> Q) {
int n, m; vector<pi> s(3);
n = N;
s[0].F = A; s[1].F = B; s[2].F = C;
m = sz(P);
for (int i = 0; i < 3; i++) s[i].S = i + 1;
srt(s);
vi ans(n);
vector<pi> edge;
DisjointSet ds(n);
for (int i = 0; i < m; i++)
{
int u, v; u = P[i]; v = Q[i];
edge.eb(u, v);
}
for (auto &[u, v] : edge)
{
if (ds.same(u, v)) continue;
ds.Union(u, v);
vc[u].pb(v); vc[v].pb(u);
}
int limit = n / 2;
int cent = getCent(0, -1, limit);
for (auto &node : vc[cent])
{
path.clear();
int tmp = getComp(node, cent);
if (tmp >= s[0].F)
{
for (int i = 0; i < s[0].F; i++) ans[path[i]] = s[0].S;
path.clear();
tmp = getComp(cent, node);
for (int i = 0; i < s[1].F; i++) ans[path[i]] = s[1].S;
for (int i = 0; i < n; i++) if (!ans[i]) ans[i] = s[2].S;
return ans;
}
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |