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 132
| #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int MAXN = 5e4 + 5; const int MAXM = 1e5 + 2e4 + 5; const int INF = 0x3f3f3f3f;
void read(int& x) { x = 0; int f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -f; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } x *= f; }
int n, m, st, ed, now, ans, tmp, cnt[MAXN], d[MAXM], cur[MAXM]; int tot = 1, head[MAXN], ver[MAXM], edge[MAXM], nxt[MAXM], fr[MAXM], scc; int dfn[MAXN], low[MAXN], stk[MAXN], vis[MAXN], tp, dn; bool ex[MAXN]; queue<int> q;
void AddEdge(int u, int v, int c) { ver[++tot] = v, fr[tot] = u, edge[tot] = c, nxt[tot] = head[u], head[u] = tot; ver[++tot] = u, fr[tot] = v, edge[tot] = 0, nxt[tot] = head[v], head[v] = tot; }
bool bfs() { memset(d, 0, sizeof(d)); while (!q.empty()) q.pop(); q.push(st), d[st] = 1, cur[st] = head[st]; while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; i; i = nxt[i]) { int v = ver[i]; if (!edge[i] || d[v] != 0) continue; q.push(v); cur[v] = head[v]; d[v] = d[u] + 1; if (v == ed) return true; } }
return false;
}
int dinic(int u, int flow) { if (u == ed) return flow; int res = flow, k, i; for (i = cur[u]; i && res; i = nxt[i]) { int v = ver[i]; if (!edge[i] || d[v] != d[u] + 1) continue; now++; k = dinic(v, min(res, edge[i])); now--; if (k == 0) d[v] = 0; edge[i] -= k, edge[i ^ 1] += k; res -= k; cur[u] = i; } return flow - res; }
void Tarjan(int u, int fa) { dfn[u] = low[u] = ++dn; ex[u] = 1, stk[++tp] = u; for (int i = head[u]; i; i = nxt[i]) { int v = ver[i]; if (!edge[i]) continue; if (!dfn[v]) { Tarjan(v, u); low[u] = min(low[u], low[v]); } else if (ex[v]) { low[u] = min(low[u], dfn[v]); } } if (low[u] == dfn[u]) { int v; scc++; do { v = stk[tp--]; vis[v] = scc; ex[v] = 0; } while (v != u); } }
int main() {
freopen("mincut.in", "r", stdin); freopen("mincut.out", "w", stdout); read(n), read(m), read(st), read(ed);
for (int i = 1, u, v, w; i <= m; i++) read(u), read(v), read(w), AddEdge(u, v, w);
while (bfs()) { while ((tmp = dinic(st, INF))) ans += tmp; }
for (int i = 1; i <= n; i++) { if (!vis[i]) { Tarjan(i, 0); } }
for (int i = 2; i <= tot; i += 2) {
if (edge[i] != 0) { printf("0 0\n"); } else { if (vis[fr[i]] != vis[ver[i]]) { if (vis[ver[i]] == vis[ed] && vis[fr[i]] == vis[st]) { printf("1 1\n"); } else { printf("1 0\n"); } } else printf("0 0\n"); } }
return 0; }
|