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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| #include <algorithm> #include <iostream> #include <fstream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cassert> using namespace std;
void ri(int&);
const int MAXN = 860, M = 8510*4, INF = 1<<29;
int n, m;
int to[M], nex[M], index[MAXN], mcap[M], cap[M], cur=0; int pn[MAXN], tmppn[MAXN];
void addEdge(int a, int b, int cap);
int q[MAXN], qh, qt, flag[MAXN], fa[MAXN], path[MAXN], FLOW[MAXN], cntf=0;
int bfs(int S, int T) { qh = 0; qt = 1; memset(flag, -1, sizeof(int)*(n+3)); flag[S] = 1; q[0] = S; while (qh < qt) { int x = q[qh++]; for (int i=index[x]; i!=-1; i=nex[i]) { if (flag[to[i]]==-1 && cap[i]>0) { flag[to[i]] = flag[x]+1; q[qt++] = to[i]; } } } return flag[T]; }
void Dinic(int S, int T) { int depth; int flow = 0; while(bfs(S, T)!=-1) { fa[depth=1] = S; while (0 < depth) { if (fa[depth] == T) { int minc = INF, mink; for (int i=1; i<depth; ++i) { if (cap[path[i]] < minc) { minc = cap[path[i]]; mink = i; } } flow += minc; for (int i=1; i<depth; ++i) { cap[path[i]] -= minc; cap[path[i]^1]+= minc; } depth = mink; } bool alive = false; for (int i=index[fa[depth]]; i!=-1; i=nex[i]) { if (flag[fa[depth]]+1==flag[to[i]] && cap[i]>0) { path[depth] = i; fa[++depth] = to[i]; alive = true; break; } } if (!alive) { flag[fa[depth]] = -1; --depth; } } } FLOW[cntf++] = flow; }
void dfs(int l, int r) { if (l==r) return; memcpy(cap, mcap, sizeof(int)*cur); Dinic(pn[l], pn[r]); int lpart = l-1, rpart = r+1; for (int i=l; i<=r; ++i) { if (flag[pn[i]]!=-1) tmppn[++lpart] = pn[i]; else tmppn[--rpart] = pn[i]; } for (int i=l; i<=r; ++i) { pn[i] = tmppn[i]; } dfs(l, lpart); dfs(rpart, r); }
int main() { freopen("cuts.in", "r", stdin); freopen("cuts.out", "w", stdout); memset(index, -1, sizeof(index)); ri(n); ri(m); for (int i=0, a, b, c; i<m; ++i) { ri(a); ri(b); ri(c); addEdge(a, b, c); addEdge(b, a, c);
} for (int i=1; i<=n; ++i) pn[i] = i;
dfs(1, n);
sort(FLOW, FLOW+cntf); int ans = 1; for (int i=1; i<cntf; ++i) { if (FLOW[i]!=FLOW[i-1]) ++ans; }
printf("%d\n", ans);
return 0; }
inline void addEdge(int a, int b, int c) { to[cur] = b; mcap[cur] = c; nex[cur] = index[a]; index[a] = cur++; to[cur] = a; mcap[cur] = 0; nex[cur] = index[b]; index[b] = cur++; }
inline void ri(int &x) { char c; while ((c=getchar())<'0' || '9'<c); x = c-'0'; while ('0'<=(c=getchar()) && c<='9') x = 10*x + c-'0';
}
|