求的最大流后依次从源点和汇点开始dfs 如果能搜到的点数之和等于总的点数,最小割唯一,否则不唯一
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
#include<stdio.h>
#include<string.h>
const int MAX=100005;
const int INF=1000000000;
struct
{
int v,c,next;
}edge[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)
{
/*加边的时候同时加两条,
一条正的,一条反的,
一般情况下反的容量是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<a)?b:a;}
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%d%d%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%d%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;
}