1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| #include <bits/stdc++.h>
using namespace std;
const int N = 2e7 + 10; const int M = 6e7 + 10;
int n, m, q;
int cnt, head[N];
struct edge { int to, nxt; edge(int v = 0, int x = 0) : to(v), nxt(x) {} };
edge e[M << 1];
void add(int u, int v) { e[++cnt] = edge(v, head[u]); head[u] = cnt; }
int dfn[N], low[N], tim;
int st[N], top;
bool vis[N];
int color, c[N];
void tarjan(int x) { dfn[x] = low[x] = ++tim; vis[x] = true; st[++top] = x; for(int i = head[x]; i; i = e[i].nxt) { int v = e[i].to; if(!dfn[v]) { tarjan(v); low[x] = min(low[x], low[v]); } else if(vis[v]) { low[x] = min(low[x], dfn[v]); } } if(dfn[x] == low[x]) { color++; while(st[top + 1] != x) { vis[st[top]] = false; c[st[top]] = color; top--; } } }
int t;
int P(int x, int y, int f) { return x + y * n + f * t; }
void build() { for(int i = 1; i <= n; i++) { for(int j = 0; j < m; j++) { add(P(i, j, 0), P(i, j + 1, 0)); add(P(i, j + 1, 1), P(i, j, 1)); } add(P(i, m, 1), P(i, m, 0)); } }
bool inrange(int x) { return x >= 0 && x <= m; }
int main() { scanf("%d%d%d", &n, &m, &q); t = (m + 1) * n; build(); while(q--) { int i, j, l, r; scanf("%d%d%d%d", &i, &j, &l, &r); for(int x = r; x < m; x++) { add(P(i, x, 1), P(i, x, 0)), add(P(j, x, 1), P(j, x, 0)); } for(int x = 0; x < r; x++) { if(inrange(x) && inrange(r - x - 1)) { add(P(i, x, 1), P(j, r - x - 1, 0)); add(P(j, x, 1), P(i, r - x - 1, 0)); } } for(int x = 0; x < l; x++) { if(inrange(x) && inrange(l - x - 1)) { add(P(i, x, 0), P(j, l - x - 1, 1)); add(P(j, x, 0), P(i, l - x - 1, 1)); } } } for(int i = 1; i <= 2 * t; i++) if(!dfn[i])tarjan(i); for(int i = 1; i <= n; i++) for(int j = 0; j <= m; j++) if(c[P(i, j, 0)] == c[P(i, j, 1)]) return printf("-1"), 0; for(int i = 1; i <= n; i++) for(int j = 0; j <= m; j++) if(c[P(i, j, 0)] < c[P(i, j, 1)]) {printf("%d ", j); break;} return 0; }
|