結果
| 問題 |
No.8071 玉虫色着色
|
| ユーザー |
|
| 提出日時 | 2020-07-28 23:09:42 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 323 ms / 2,000 ms |
| コード長 | 4,032 bytes |
| コンパイル時間 | 1,221 ms |
| コンパイル使用メモリ | 121,748 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-22 07:53:42 |
| 合計ジャッジ時間 | 17,094 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 13 TLE * 1 |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
class MaxFlow(Capa) {
enum Capa wEPS = 0;
enum Capa wINF = 10^^9;
int n, m;
int[][] g;
int[] zu;
Capa[] capa;
Capa tof;
int[] lev, see, que;
this(int n) {
this.n = n; m = 0; g = new int[][n]; zu = []; capa = [];
lev = new int[n]; see = new int[n]; que = new int[n];
}
void addEdge(int u, int v, Capa w0, Capa w1 = 0) {
g[u] ~= m; zu ~= v; capa ~= w0; ++m;
g[v] ~= m; zu ~= u; capa ~= w1; ++m;
}
Capa augment(int src, int ink, Capa flo) {
if (src == ink) return flo;
foreach (i; g[src][see[src] .. $]) {
if (capa[i] > wEPS && lev[src] < lev[zu[i]]) {
Capa f = augment(zu[i], ink, min(flo, capa[i]));
if (f > wEPS) { capa[i] -= f; capa[i ^ 1] += f; return f; }
}
++see[src];
}
return 0;
}
bool dinic(int src, int ink, Capa flo = wINF) {
for (tof = 0; tof + wEPS < flo; ) {
int qb, qe;
lev[] = -1;
dinicBFS:
for (lev[src] = 0, que[qe++] = src; qb != qe; ) {
const u = que[qb++];
foreach (i; g[u]) {
const v = zu[i];
if (capa[i] > wEPS && lev[v] == -1) {
lev[v] = lev[u] + 1; que[qe++] = v;
if (v == ink) break dinicBFS;
}
}
}
if (lev[ink] == -1) return false;
see[] = 0;
for (; ; ) {
Capa f = augment(src, ink, flo - tof);
if (f <= wEPS) break;
tof += f;
}
}
return true;
}
}
void main() {
try {
for (; ; ) {
const L = readInt();
const R = readInt();
const M = readInt();
auto A = new int[M];
auto B = new int[M];
foreach (i; 0 .. M) {
A[i] = readInt();
B[i] = L + readInt();
}
int color;
auto ans = new int[M];
ans[] = -1;
for (color = 0; ; ++color) {
auto deg = new int[L + R];
foreach (i; 0 .. M) {
if (ans[i] == -1) {
++deg[A[i]];
++deg[B[i]];
}
}
const minDeg = deg.minElement;
if (minDeg == 0) {
break;
}
auto mf = new MaxFlow!int(L + R + 2);
auto ids = new int[M];
ids[] = -1;
foreach (u; 0 .. L) {
mf.addEdge(L + R + 0, u, deg[u] - minDeg + 1);
}
foreach (v; L .. L + R) {
mf.addEdge(v, L + R + 1, deg[v] - minDeg + 1);
}
foreach (i; 0 .. M) {
if (ans[i] == -1) {
ids[i] = mf.m;
mf.addEdge(A[i], B[i], 1);
}
}
mf.dinic(L + R + 0, L + R + 1);
foreach (i; 0 .. M) {
if (ans[i] == -1) {
if (mf.capa[ids[i]] == 0) {
ans[i] = color;
}
}
}
}
writeln(color);
foreach (i; 0 .. M) {
writeln(ans[i]);
}
}
} catch (EOFException e) {
}
}