判断割是否唯一zoj2587
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
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=808; int head[N],tot,S,T; int q[N],dis[N],n,m; bool vis[N]; struct node { int next,v,w; } e[N*50]; void add(int u,int v,int w) { e[tot].v=v; e[tot].w=w; e[tot].next=head[u]; head[u]=tot++; } bool bfs() { memset(dis,-1,sizeof(dis)); dis[S]=0; int l=0,r=0; q[r++]=S; while(l<r) { int u=q[l++]; for(int i=head[u]; ~i; i=e[i].next) { int v=e[i].v; if(dis[v]==-1&&e[i].w>0) { q[r++]=v; dis[v]=dis[u]+1; if(v==T) return true; } } } return false; } int dfs(int s,int low) { if(s==T||!low) return low; int ans=low,a; for(int i=head[s]; ~i; i=e[i].next) { if(e[i].w>0&&dis[e[i].v]==dis[s]+1&&(a=dfs(e[i].v,min(e[i].w,ans)))) { e[i].w-=a; e[i^1].w+=a; ans-=a; if(!ans) return low; } } if(low==ans) dis[s]=-1; return low-ans; } void dfs(int &cnt,int u,int k){ for(int i=head[u];~i;i=e[i].next){ int v=e[i].v; if(!vis[v]&&e[i^k].w) { vis[v]=1; ++cnt; dfs(cnt,v,k); } } } int main(){ int x,y,z; while(scanf("%d%d%d%d",&n,&m,&S,&T)!=EOF){ if(n+m+S+T==0) break; memset(head,-1,sizeof(head)); tot=0; while(m--){ scanf("%d%d%d",&x,&y,&z); add(x,y,z); add(y,x,z); } while(bfs()) dfs(S,1008611); int cnt1=1,cnt2=1; memset(vis,0,sizeof(vis)); dfs(cnt1,S,0); memset(vis,0,sizeof(vis)); dfs(cnt2,T,1); if(cnt1>1) --cnt1; if(cnt2>1) --cnt2; printf("%s\n",cnt1+cnt2==n?"UNIQUE":"AMBIGUOUS"); } }