看最小割是否唯一。就是在残余网络上扫一遍看是否所有边都能被访问到
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define inf 10000000 const int maxn=1500; int n,m; int from[maxn],to[maxn]; int level[maxn],que[maxn]; int head[maxn],lon; int min(int a,int b) { if(a<b) return a; else return b; } struct edge { int next,from,to,c; }e[45000]; void edgeini() { memset(head,-1,sizeof(head)); lon=-1; } void edgemake(int from,int to,int c) { e[++lon].c=c; e[lon].from=from; e[lon].to=to; e[lon].next=head[from]; head[from]=lon; } void make(int from,int to,int c) { edgemake(from,to,c); edgemake(to,from,0); } bool makelevel(int s,int t) { memset(level,0,sizeof(level)); int front=1,end=0; que[++end]=s; level[s]=1; while(front<=end) { int u=que[front++]; if(u==t) return true; for(int k=head[u];k!=-1;k=e[k].next) { int v=e[k].to; if(!level[v]&&e[k].c) { que[++end]=v; level[v]=level[u]+1; } } } return false; } int dfs(int now,int t,int maxf) { if(now==t||maxf==0) return maxf; int ret=0; for(int k=head[now];k!=-1;k=e[k].next) { int u=e[k].to; if(level[u]==level[now]+1&&e[k].c) { int f=dfs(u,t,min(e[k].c,maxf-ret)); e[k].c-=f; e[k^1].c+=f; ret+=f; if(ret==maxf) return ret; } } if(ret==0) level[now]=0; return ret; } int maxflow(int s,int t) { int ret=0; while(makelevel(s,t)) { ret+=dfs(s,t,inf); } return ret; } void dfs1(int now) { int i; from[now]=1; for(i=head[now];i!=-1;i=e[i].next) if(!from[e[i].to]&&e[i].c) dfs1(e[i].to); } void dfs2(int now) { int i; to[now]=1; for(i=head[now];i!=-1;i=e[i].next) if(!to[e[i].to]&&e[i^1].c) dfs2(e[i].to); } int main() { int s,t; while(1) { int i,j; int u,v,w; int flag=0; scanf("%d%d%d%d",&n,&m,&s,&t); if(n==0) break; edgeini(); for(i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&w); make(u,v,w); make(v,u,w); } int flow=maxflow(s,t); memset(from,0,sizeof(from)); memset(to,0,sizeof(to)); dfs1(s); dfs2(t); for(i=1;i<=n;i++) if(from[i]==0&&to[i]==0) flag=1; if(flag) printf("AMBIGUOUS "); else printf("UNIQUE "); } return 0; }