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>
using namespace std;
using ll = long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<ld, ld>;
#define mp make_pair
#define ff first
#define ss second
#define ar array
template<class T> using V = vector<T>;
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<ld>;
using vs = V<str>;
using vpi = V<pi>;
using vpl = V<pl>;
using vpd = V<pd>;
#define sz(x) (int)((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for(int i = (b) - 1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define rep(a) F0R(_, a)
#define trav(a, x) for(auto& a : x)
template<class T> bool ckmin(T& a, const T& b) { return (b < a ? a = b, 1 : 0); }
template<class T> bool ckmax(T& a, const T& b) { return (b > a ? a = b, 1 : 0); }
V<vi> adj;
struct DSU {
vector<int> E;
void init(int n) { E = vector<int>(n, -1); }
int parent(int x) { return E[x] < 0 ? x : E[x] = parent(E[x]); }
bool sameset(int x, int y) { return parent(x) == parent(y); }
int size(int x) { return -E[parent(x)]; }
bool unite(int x, int y) {
x = parent(x);
y = parent(y);
if(x == y) {
return false;
}
if(E[x] > E[y]) {
swap(x, y);
}
E[x] += E[y];
E[y] = x;
return true;
}
}D;
vi ans;
void color(int s, int& cnt, int c) {
if(!cnt) return;
--cnt;
ans[s] = c;
trav(u, adj[s]) {
if(!cnt) break;
if(ans[u] == 0) color(u, cnt, c);
}
}
vector<int> find_split(int N, int a, int b, int c, vector<int> p, vector<int> q) {
D.init(N);
ans = vi(N);
adj.rsz(N);
int M = sz(p);
vpi comp;
F0R(i, M) {
if(!D.unite(p[i], q[i])) continue;
adj[p[i]].pb(q[i]);
adj[q[i]].pb(p[i]);
}
vi res;
{
set<int> t;
F0R(i, N) {
t.ins(D.parent(i));
}
trav(u, t) {
comp.eb(D.size(u), u);
}
sor(comp);
}
int mn = min({a, b, c});
int mn2 = a + b + c - mn - max({a, b, c});
int mncolor = (a == mn ? 1 : (b == mn ? 2 : 3));
int mn2color = (a == mn2 && mncolor != 1 ? 1 : (b == mn2 && mncolor != 2 ? 2 : 3)); // sus;
auto x = lb(all(comp), mp(mn, 0)), y = lb(all(comp), mp(mn2, 0));
if(x != end(comp) && y != end(comp)) {
if(x == y) {
++y;
if(y != end(comp)) {
color((*x).ss, mn, mncolor);
color((*y).ss, mn2, mn2color);
F0R(i, N) {
if(ans[i] == 0) {
ans[i] = 6 - mncolor - mn2color;//;
}
}
return ans;
}
}
//
}
if(comp.bk.ff >= mn + mn2) {
F0R(i, N) {
if(D.parent(i) == comp.bk.ss && sz(adj[i]) == 1) {
color(i, mn, mncolor);
}
}
F0R(i, N) {
if(D.parent(i) == comp.bk.ss && ans[i] == 0) {
color(i, mn2, mn2color);
}
}
F0R(i, N) {
if(ans[i] == 0) {
ans[i] = 6 - mncolor - mn2color;//;
}
}
}
return ans;
}
/*
int main() {
int n, m, a, b, c;
assert(5 == scanf("%d%d%d%d%d", &n, &m, &a, &b, &c));
vector<int> p(m), q(m);
for (int i=0; i<m; i++)
assert(2 == scanf("%d%d", &p[i], &q[i]));
fclose(stdin);
vector<int> result = find_split(n, a, b, c, p, q);
for (int i=0; i<(int)result.size(); i++)
printf("%s%d", ((i>0)?" ":""), result[i]);
printf("\n");
fclose(stdout);
return 0;
}*/
# | 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... |