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],如果SUT不包含所有点,那么最小割不唯一。假设点i不被SUT包含,那么残留网络中s不能到达i,i不能到达t,即进入i的边和从i出去的边都满流,假设某条进入i的边x满流,这些流量从若干条边y流出i,那么,如果选x为割边,或者选所有对应的y为割边,不会影响最大流,即最小割容量不变,最小割也就不唯一。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1010;
const int M=50000;
const int inf=(1<<28);
int head[N];
struct Edge
{
int v,next,w;
} edge[M];
int cnt,n,s,t;
void addedge(int u,int v,int w)
{
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].v=u;
edge[cnt].w=0;
edge[cnt].next=head[v];
head[v]=cnt++;
}
int sap()
{
int pre[N],cur[N],dis[N],gap[N];
int flow=0,aug=inf,u;
bool flag;
for(int i=0; i<n; i++)
{
cur[i]=head[i];
gap[i]=dis[i]=0;
}
gap[s]=n;
u=pre[s]=s;
while(dis[s]<n)
{
flag=0;
for(int &j=cur[u]; j!=-1; j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].w>0&&dis[u]==dis[v]+1)
{
flag=1;
if(edge[j].w<aug) aug=edge[j].w;
pre[v]=u;
u=v;
if(u==t)
{
flow+=aug;
while(u!=s)
{
u=pre[u];
edge[cur[u]].w-=aug;
edge[cur[u]^1].w+=aug;
}
aug=inf;
}
break;
}
}
if(flag) continue;
int mindis=n;
for(int j=head[u]; j!=-1; j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].w>0&&dis[v]<mindis)
{
mindis=dis[v];
cur[u]=j;
}
}
if((--gap[dis[u]])==0)
break;
gap[dis[u]=mindis+1]++;
u=pre[u];
}
return flow;
}
int vis1[1000];
int dfs1(int i)
{
int j;
vis1[i]=1;
for(j=head[i];j!=-1;j=edge[j].next)
{
int t=edge[j].v;
if(vis1[t]==0&&edge[j].w) dfs1(t);//j
}
}
int vis2[1000];
int dfs2(int i)
{
int j;
vis2[i]=1;
for(j=head[i];j!=-1;j=edge[j].next)
{
int t=edge[j].v;
if(vis2[t]==0&&edge[j^1].w) dfs2(t);//j
}
}
int main()
{
int m;
while(scanf("%d%d%d%d",&n,&m,&s,&t),n||m||s||t)
{
s--,t--;
cnt=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=m;i++)
{
int u,v,w;scanf("%d%d%d",&u,&v,&w);u--,v--;
addedge(u,v,w);
addedge(v,u,w);
}
int ans=sap();
memset(vis1,0,sizeof(vis1));
dfs1(s);
memset(vis2,0,sizeof(vis2));
dfs2(t);
bool flag=false;
for(int i=0;i<n;i++)
if((vis1[i]&&vis2[i])||(!vis1[i]&&!vis2[i]))
{
flag=true;
break;
}
if(!flag) printf("UNIQUE\n");
else printf("AMBIGUOUS\n");
}
return 0;
}