#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } namespace MF { const int LIM_N = 200010; const int LIM_M = 200010; typedef int wint; const wint wEPS = 0; const wint wINF = 1001001001; int n, m, ptr[LIM_N], nxt[LIM_M * 2], zu[LIM_M * 2]; wint capa[LIM_M * 2], tof; int lev[LIM_N], see[LIM_N], que[LIM_N], *qb, *qe; void init(int _n) { n = _n; m = 0; fill(ptr, ptr + n, -1); } void ae(int u, int v, wint w0, wint w1 = 0) { nxt[m] = ptr[u]; ptr[u] = m; zu[m] = v; capa[m] = w0; ++m; nxt[m] = ptr[v]; ptr[v] = m; zu[m] = u; capa[m] = w1; ++m; } wint augment(int src, int ink, wint flo) { if (src == ink) return flo; for (int &i = see[src]; ~i; i = nxt[i]) if (capa[i] > wEPS && lev[src] < lev[zu[i]]) { const wint f = augment(zu[i], ink, min(flo, capa[i])); if (f > wEPS) { capa[i] -= f; capa[i ^ 1] += f; return f; } } return 0; } bool solve(int src, int ink, wint flo = wINF) { for (tof = 0; tof + wEPS < flo; ) { qb = qe = que; fill(lev, lev + n, -1); for (lev[*qe++ = src] = 0, see[src] = ptr[src]; qb != qe; ) { const int u = *qb++; for (int i = ptr[u]; ~i; i = nxt[i]) if (capa[i] > wEPS) { const int v = zu[i]; if (lev[v] == -1) { lev[*qe++ = v] = lev[u] + 1; see[v] = ptr[v]; if (v == ink) goto au; } } } return false; au: for (wint f; (f = augment(src, ink, flo - tof)) > wEPS; tof += f); } return true; } } int L, R, M; vector A, B; int main() { for (; ~scanf("%d%d%d", &L, &R, &M); ) { A.resize(M); B.resize(M); for (int i = 0; i < M; ++i) { scanf("%d%d", &A[i], &B[i]); B[i] += L; } int color; vector ans(M, -1); for (color = 0; ; ++color) { vector deg(L + R, 0); for (int i = 0; i < M; ++i) { if (ans[i] == -1) { ++deg[A[i]]; ++deg[B[i]]; } } const int minDeg = *min_element(deg.begin(), deg.end()); if (minDeg == 0) { break; } MF::init(L + R + 2); vector ids(M, -1); for (int u = 0; u < L; ++u) { MF::ae(L + R + 0, u, deg[u] - minDeg + 1); } for (int v = L; v < L + R; ++v) { MF::ae(v, L + R + 1, deg[v] - minDeg + 1); } for (int i = 0; i < M; ++i) { if (ans[i] == -1) { ids[i] = MF::m; MF::ae(A[i], B[i], 1); } } MF::solve(L + R + 0, L + R + 1); for (int i = 0; i < M; ++i) { if (ans[i] == -1) { if (MF::capa[ids[i]] == 0) { ans[i] = color; } } } } printf("%d\n", color); for (int i = 0; i < M; ++i) { printf("%d\n", ans[i]); } } return 0; }