QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#97670 | #6329. Colorful Graph | yzc2005 | ML | 2ms | 5808kb | C++14 | 3.7kb | 2023-04-17 21:02:40 | 2023-04-17 21:02:42 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
using pii = pair<int, int>;
using i64 = long long;
#define rep(i, a, b) for (int i = a, I = b; i <= I; ++i)
#define per(i, a, b) for (int i = a, I = b; i >= I; --i)
template<typename T> void up(T &x, T y) { if (x < y) x = y; }
template<typename T> void down(T &x, T y) { if (x > y) x = y; }
const int N = 7005, inf = 1e9;
int n, m, a[N], b[N], c[N], bel[N], stk[N], to[N], top, cnt, d[N];
vector<int> e1[N], e2[N];
bool vis[N];
void dfs1(int u) {
vis[u] = 1;
for (auto v : e2[u]) if (!vis[v]) dfs1(v);
stk[++top] = u;
}
void dfs2(int u, int c) {
bel[u] = c;
for (auto v : e1[u]) if (!bel[v]) dfs2(v, c);
}
vector<int> A, B;
namespace flow {
const int N = 14005, M = 123456;
int n, s, t, d[N], que[N], cap[M], res;
int cnt = 1, head[N], cur[N], to[M], nxt[M], val[M];
void addedge(int u, int v, int w) {
to[++cnt] = v;
cap[cnt] = val[cnt] = w;
nxt[cnt] = head[u];
head[u] = cnt;
}
void add(int u, int v, int w) {
addedge(u, v, w);
addedge(v, u, 0);
}
bool bfs() {
int l = 1, r = 0;
rep(i, 0, n) cur[i] = head[i], d[i] = 0;
que[++r] = s, d[s] = 1;
while (l <= r) {
int u = que[l++];
for (int i = head[u]; i; i = nxt[i]) {
int v = to[i], w = val[i];
if (d[v] || !w) continue;
d[v] = d[u] + 1;
que[++r] = v;
}
}
return d[t];
}
int dfs(int u, int flow) {
if (u == t) return flow;
int rest = flow;
for (int &i = cur[u]; i; i = nxt[i]) {
int v = to[i], w = val[i];
if (d[v] != d[u] + 1 || !w) continue;
int k = dfs(v, min(flow, w));
if (!k) d[v] = 0;
rest -= k;
val[i] -= k, val[i ^ 1] += k;
if (!rest) break;
}
return flow - rest;
}
int dinic() {
res = 0;
while (bfs()) res += dfs(s, inf);
return res;
}
struct edge {
int to, nxt, val;
} e[M];
int hd[N], tot;
void get_path(int u) {
for (int &i = hd[u]; i; ) {
int v = e[i].to;
if (!--e[i].val) i = e[i].nxt;
get_path(v);
if (v == t) B.push_back(u);
if (u == s) A.push_back(v);
}
}
void solve() {
rep(u, 0, n) {
for (int i = head[u]; i; i = nxt[i]) {
if (!cap[i]) continue;
int f = cap[i] - val[i];
if (!f) continue;
int v = to[i];
e[++tot] = {v, hd[u], f}, hd[u] = tot;
}
}
e[++tot] = {s, hd[t], res}, hd[t] = tot;
get_path(s);
}
}
int main() {
// freopen("1.in", "r", stdin);
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
rep(i, 1, m) {
cin >> a[i] >> b[i];
e1[a[i]].push_back(b[i]);
e2[b[i]].push_back(a[i]);
}
rep(i, 1, n) if (!vis[i]) dfs1(i);
per(i, n, 1) {
int u = stk[i];
if (!bel[u]) dfs2(u, ++cnt);
}
flow::t = flow::n = 2 * cnt + 1;
rep(i, 1, cnt) flow::add(flow::s, i, 1), flow::add(i + cnt, flow::t, 1);
rep(i, 1, cnt) flow::add(i + cnt, i, inf);
rep(i, 1, m) {
int u = bel[a[i]], v = bel[b[i]];
if (u == v) continue;
flow::add(u, v + cnt, inf);
}
int res = flow::dinic();
flow::solve();
assert((int) A.size() == res);
rep(i, 0, res - 1) {
to[A[i]] = B[i] - cnt, ++d[B[i] - cnt];
assert(d[B[i] - cnt] == 1);
}
int col = 0;
rep(u, 1, cnt) {
if (d[u]) continue;
c[u] = ++col;
int x = u;
while (to[x]) x = to[x], c[x] = col;
}
rep(i, 1, n) cout << c[bel[i]] << " \n"[i == n];
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 5784kb
input:
5 5 1 4 2 3 1 3 2 5 5 1
output:
2 2 2 1 2
result:
ok AC
Test #2:
score: 0
Accepted
time: 2ms
memory: 5808kb
input:
5 7 1 2 2 1 4 3 5 1 5 4 4 1 4 5
output:
2 2 1 2 2
result:
ok AC
Test #3:
score: 0
Accepted
time: 2ms
memory: 5800kb
input:
8 6 6 1 3 4 3 6 2 3 4 1 6 4
output:
4 4 4 4 3 4 2 1
result:
ok AC
Test #4:
score: -100
Memory Limit Exceeded
input:
7000 6999 4365 4296 2980 3141 6820 4995 4781 24 2416 5844 2940 2675 3293 2163 3853 5356 262 6706 1985 1497 5241 3803 353 1624 5838 4708 5452 3019 2029 6161 3849 4219 1095 1453 4268 4567 1184 1857 2911 3977 1662 2751 6353 6496 2002 6628 1407 4623 425 1331 4445 4277 1259 3165 4994 1044 2756 5788 5496 ...