N supercomputers in the United States of Antarctica are connected into a network. A network has a simple topology: M different pairs of supercomputers are connected to each other by an optical fibre. All connections are two-way, that is, they can be used in both directions. Data can be transmitted from one computer to another either directly by a fibre, or using some intermediate computers.
A group of terrorists is planning to attack the network. Their goal is to separate two main computers of the network, so that there is no way to transmit data from one of them to another. For each fibre the terrorists have calculated the sum of money they need to destroy the fibre. Of course, they want to minimize the cost of the operation, so it is required that the total sum spent for destroying the fibres was minimal possible.
Now the leaders of the group wonder whether there is only one way to do the selected operation. That is, they want to know if there are no two different sets of fibre connections that can be destroyed, such that the main supercomputers cannot connect to each other after it and the cost of the operation is minimal possible.
Input
The input file consists of several cases. In each case, the first line of the input file contains N, M, A and B (2 <= N <= 800, 1 <= M <= 10000, 1 <= A,B <= N, A != B), specifying the number of supercomputers in the network, the number of fibre connections, and the numbers of the main supercomputers respectively. A case with 4 zeros indicates the end of file.
Next M lines describe fibre connections. For each connection the numbers of the computers it connects are given and the cost of destroying this connection. It is guaranteed that all costs are non-negative integer numbers not exceeding 105, no two computers are directly connected by more than one fibre, no fibre connects a computer to itself and initially there is the way to transmit data from one main supercomputer to another.
Output
If there is only one way to perform the operation, output "UNIQUE" in a single line. In the other case output "AMBIGUOUS".
Sample Input
4 4 1 2 1 2 1 2 4 2 1 3 2 3 4 1 4 4 1 2 1 2 1 2 4 1 1 3 2 3 4 1 0 0 0 0
Sample Output
UNIQUE AMBIGUOUS
然后在残留网络中分别从源汇开始dfs一次,
找出最小割[S,T],如果[S,T]不包含所有点,那么最小割不唯一。
假设点i不被[S,T]包含,
那么残留网络中 s 不能到达 i , i 不能到达 t ,即进入 i 的边和从 i 出去的边都满流,
假设某条进入 i 的边 x 满流,
这些流量从若干条边 y 流出 i ,那么,如果选 x 为割边,或者选所有对应的 y 为割边,
不会影响最大流,即最小割容量不变,最小割也就不唯一
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 888, M = 22222;
struct Node {
int u, v, cap;
int next;
}edge[M];
int n, m, S, T, tot, head[N], cur[N], lev[N], q[N], s[N];
bool vis1[N], vis2[N];
int ret1, ret2;
void init()
{
tot = 0; memset(head, -1, sizeof(head));
}
void add(int u, int v, int c)
{
edge[tot].u = u; edge[tot].v = v; edge[tot].cap = c;
edge[tot].next = head[u]; head[u] = tot++;
}
bool bfs()
{
memset(lev, -1, sizeof(lev));
int fron = 0, rear = 0;
q[rear++] = S;
lev[S] = 0;
while(fron < rear) {
int u = q[fron%N]; fron++;
for(int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].v;
if(edge[i].cap && lev[v] == -1) {
lev[v] = lev[u] + 1;
q[rear%N] = v; rear++;
if(v == T) return true;
}
}
}
return false;
}
void Dinic()
{
while(bfs()) {
memcpy(cur, head, sizeof(head));
int u = S, top = 0;
while(1) {
if(u == T) {
int mini = INF, loc;
for(int i = 0; i < top; i++) {
if(edge[s[i]].cap < mini) {
mini = edge[s[i]].cap;
loc = i;
}
}
for(int i = 0; i < top; i++) {
edge[s[i]].cap -= mini;
edge[s[i]^1].cap += mini;
}
top = loc;
u = edge[s[top]].u;
}
int &i = cur[u];
for(; i != -1; i = edge[i].next) {
int v = edge[i].v;
if(edge[i].cap && lev[v] == lev[u] + 1) break;
}
if(i != -1) {
s[top++] = i;
u = edge[i].v;
}
else {
if(!top) break;
lev[u] = -1;
u = edge[s[--top]].u;
}
}
}
}
void dfs1(int x)
{
vis1[x] = 1;
ret1++;
for(int i = head[x]; ~i; i = edge[i].next) {
int v = edge[i].v;
if(!vis1[v] && edge[i].cap) dfs1(v);
}
}
void dfs2(int x)
{
vis2[x] = 1;
ret2++;
for(int i = head[x]; ~i; i = edge[i].next) {
int v = edge[i].v;
if(!vis2[v] && edge[i^1].cap) dfs2(v);
}//注意,要判断的是正向弧
}
int main()
{
#ifdef J_Sure
// freopen("000.in", "r", stdin);
// freopen(".out", "w", stdout);
#endif
while(~scanf("%d%d%d%d", &n, &m, &S, &T)) {
if(!n && !m && !S && !T) break;
int u, v, c;
init();
for(int j = 0; j < m; j++) {
scanf("%d%d%d", &u, &v, &c);
add(u, v, c); add(v, u, c);
}
Dinic();
for(int i = 1; i <= n; i++) {
vis1[i] = vis2[i] = 0;
}
ret1 = ret2 = 0;
dfs1(S); dfs2(T);
if(n == ret1 + ret2) puts("UNIQUE");
else puts("AMBIGUOUS");
}
return 0;
}