It is very bare to determine whether the minimum cut is unique. The determination method is to first perform the maximum flow for the minimum cut, and then traverse all the points that can be reached from the source point and the sink point to see if all the points are covered. If all the points are covered, it is unique, otherwise it is not unique.
# Include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <climits> # include <string> # include <iostream> # include <map> # include <cstdlib> # include <list> # include <set> # include <queue> # include <stack> using namespace STD; typedef long ll; const int maxn = 800 + 5; const int maxm = 80000; const int INF = int_max/2; int n, m, S, T; int first [maxn], NXT [maxm]; int ecnt, V [maxm], Cap [maxm]; I Nt q [maxn], QS, QE; bool vis [maxn]; void Adde (INT Uu, int VV, int ww) {v [ecnt] = vv; cap [ecnt] = WW; NXT [ecnt] = first [UU]; first [UU] = ecnt ++; V [ecnt] = Uu; CAP [ecnt] = 0; NXT [ecnt] = first [VV]; first [VV] = ecnt ++;} int level [maxn]; bool BFS () {memset (Level, 0, sizeof (level); Qs = Qe = 0; Q [QE ++] = s; level [s] = 1; while (QS <QE) {int now = Q [QS ++]; If (now = T) break; For (INT I = first [now]; ~ I; I = NXT [I]) {If (Cap [I] & level [V [I] = 0) {q [QE ++] = V [I]; level [V [I] = level [now] + 1 ;}} return level [T];} int DFS (INT now, int alpha) {If (now = T) return Alpha; int sum = 0; For (INT I = first [now]; ~ I & Alpha; I = NXT [I]) {If (Cap [I] & level [V [I] = level [now] + 1) {int ret = DFS (V [I], min (alpha, Cap [I]); sum + = ret; alpha-= ret; CAP [I]-= ret; cap [I ^ 1] + = ret;} If (sum = 0) level [now] =-1; return sum;} void solve () {// first perform the maximum stream while (BFS () DFS (S, INF); // perform the BFS memset (VIS, 0, 0, sizeof (VIS); Qs = Qe = 0; Q [QE ++] = s; vis [s] = true; while (QS <QE) {int now = Q [QS ++]; for (INT I = Fi RST [now]; ~ I; I = NXT [I]) {If (Cap [I] &! Vis [V [I]) {vis [V [I] = true; Q [QE ++] = V [I] ;}} Qs = Qe = 0; Q [QE ++] = T; vis [T] = true; while (QS <QE) {int now = Q [QS ++]; for (INT I = first [now]; ~ I; I = NXT [I]) {If (Cap [I ^ 1] &! Vis [V [I]) {vis [V [I] = true; Q [QE ++] = V [I] ;}} for (INT I = 1; I <= N; I ++) {If (! Vis [I]) {puts ("ambiguous"); Return ;}} puts ("unique") ;} int main () {While (scanf ("% d", & N, & M, & S, & T), n) {memset (first,-1, sizeof (first); ecnt = 0; For (INT I = 0; I <m; I ++) {int A, B, C; scanf ("% d", & A, & B, & C); Adde (A, B, C); Adde (B, A, C );} solve ();} return 0 ;}