QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#97672 | #6329. Colorful Graph | yzc2005 | WA | 1ms | 4092kb | C++14 | 4.0kb | 2023-04-17 21:07:39 | 2023-04-17 21:07:39 |
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 get_path(int u) {
for (int &i = hd[u]; i; ) {
int v = e[i].to;
if (!--e[i].val) i = e[i].nxt;
if (v == t) B.push_back(u);
if (u == s) A.push_back(v);
get_path(v);
return;
}
}
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;
}
}
while (res--) get_path(s);
// 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: 0
Wrong Answer
time: 1ms
memory: 4092kb
input:
5 5 1 4 2 3 1 3 2 5 5 1
output:
result:
wrong output format Unexpected end of file - int32 expected