Returns the largest stream from the source and sink. If the sum of the points that can be searched is equal to the total number of points, the minimum cut is unique. Otherwise, the result is not unique.
View Code
# Include <stdio. h>
# Include <string. h>
Const int MAX = 100005;
Constint INF = 1000000000;
Struct
{
Int v, c, next;
} Edge [2, 1000000];
Int E, head [MAX];
Int gap [MAX], cur [MAX];
Int pre [MAX], dis [MAX];
Void add_edge (int s, int t, int c, int cc)
{
/* Add two edges at the same time,
One is positive, one is opposite,
Generally, the reverse capacity is 0 */
Edge [E]. v = t; edge [E]. c = c;
Edge [E]. next = head [s];
Head [s] = E ++;
Edge [E]. v = s; edge [E]. c = cc;
Edge [E]. next = head [t];
Head [t] = E ++;
}
Int min (int a, int B) {return (a =-1 | B <)? B: ;}
Int SAP (int s, int t, int n)
{
Memset (gap, 0, sizeof (gap ));
Memset (dis, 0, sizeof (dis ));
Int I;
For (I = 0; I <n; I ++) cur [I] = head [I];
Int u = pre [s] = s, maxflow = 0, aug =-1, v;
Gap [0] = n;
While (dis [s] <n)
{
Loop: for (I = cur [u]; I! =-1; I = edge [I]. next)
{
V = edge [I]. v;
If (edge [I]. c> 0 & dis [u] = dis [v] + 1)
{
Aug = min (aug, edge [I]. c );
Pre [v] = u;
Cur [u] = I;
U = v;
If (u = t)
{
For (u = pre [u]; v! = S; v = u, u = pre [u])
{
Edge [cur [u]. c-= aug;
Edge [cur [u] ^ 1]. c + = aug;
}
Maxflow + = aug;
Aug =-1;
}
Goto loop;
}
}
Int mindis = n;
For (I = head [u]; I! =-1; I = edge [I]. next)
{
V = edge [I]. v;
If (edge [I]. c> 0 & dis [v] <mindis)
{
Cur [u] = I;
Mindis = dis [v];
}
}
If (-- gap [dis [u]) = 0) break;
Gap [dis [u] = mindis + 1] ++;
U = pre [u];
}
Return maxflow;
}
Int num1, num2;
Bool flag [1000];
Void dfs1 (int u)
{
Flag [u] = 1;
Num1 ++;
For (int I = head [u]; I! =-1; I = edge [I]. next)
{
Int y = edge [I]. v;
If (edge [I]. c & flag [y] = 0)
Dfs1 (y );
}
}
Void dfs2 (int u)
{
Flag [u] = 1;
Num2 ++;
For (int I = head [u]; I! =-1; I = edge [I]. next)
{
Int y = edge [I]. v;
If (edge [I ^ 1]. c & flag [y] = 0)
Dfs2 (y );
}
}
Int main ()
{
Int n, m, s, t;
Int I, j;
While (scanf ("% d", & n, & m, & s, & t ), (m | n | s | t ))
{
Memset (head,-1, sizeof (head ));
E = 0;
Int x, y, c;
For (I = 1; I <= m; I ++)
{
Scanf ("% d", & x, & y, & c );
Add_edge (x, y, c, 0 );
Add_edge (y, x, c, 0 );
}
SAP (s, t, n );
Num1 = 0, num2 = 0;
Memset (flag, 0, sizeof (flag ));
Dfs1 (s );
Memset (flag, 0, sizeof (flag ));
Dfs2 (t );
If (num1 + num2 = n) printf ("UNIQUE \ n ");
Else printf ("AMBIGUOUS \ n ");
}
Return 0;
}