[ZOJ2587]Unique Attack

vjudge

sol

最小割判定唯一性。
只要做完一个任意最小割后,判断一下是不是所有点都要么和\(S\)相连,要么和\(T\)相连。
只要两边各一次\(dfs\)就行了。

code

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int gi()
{
	int x=0,w=1;char ch=getchar();
	while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
	if (ch=='-') w=0,ch=getchar();
	while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return w?x:-x;
}
const int N = 1005;
const int inf = 1e9;
struct edge{int to,nxt,w;}a[N*20];
int n,m,S,T,head[N],cnt,dep[N],cur[N],vis1[N],vis2[N],cnt1,cnt2;
queue<int>Q;
void init()
{
	memset(head,0,sizeof(head));
	memset(vis1,0,sizeof(vis1));
	memset(vis2,0,sizeof(vis2));
	cnt=1;cnt1=cnt2=0;
}
void link(int u,int v,int w)
{
	a[++cnt]=(edge){v,head[u],w};
	head[u]=cnt;
	a[++cnt]=(edge){u,head[v],w};
	head[v]=cnt;
}
bool bfs()
{
	memset(dep,0,sizeof(dep));
	dep[S]=1;Q.push(S);
	while (!Q.empty())
	{
		int u=Q.front();Q.pop();
		for (int e=head[u];e;e=a[e].nxt)
			if (a[e].w&&!dep[a[e].to])
				dep[a[e].to]=dep[u]+1,Q.push(a[e].to);
	}
	return dep[T];
}
int dfs(int u,int f)
{
	if (u==T) return f;
	for (int &e=cur[u];e;e=a[e].nxt)
		if (a[e].w&&dep[a[e].to]==dep[u]+1)
		{
			int tmp=dfs(a[e].to,min(a[e].w,f));
			if (tmp) {a[e].w-=tmp;a[e^1].w+=tmp;return tmp;}
		}
	return 0;
}
void Dinic()
{
	while (bfs())
	{
		for (int i=1;i<=n;++i) cur[i]=head[i];
		while (dfs(S,inf)) ;
	}
}
void dfs1(int u)
{
	++cnt1;
	for (int e=head[u];e;e=a[e].nxt)
		if (!vis1[a[e].to]&&a[e].w)
			vis1[a[e].to]=1,dfs1(a[e].to);
}
void dfs2(int u)
{
	++cnt2;
	for (int e=head[u];e;e=a[e].nxt)
		if (!vis2[a[e].to]&&a[e^1].w)
			vis2[a[e].to]=1,dfs2(a[e].to);
}
int main()
{
	while (scanf("%d%d%d%d",&n,&m,&S,&T),n+m+S+T)
	{
		init();
		for (int i=1;i<=m;++i)
		{
			int u=gi(),v=gi(),w=gi();
			link(u,v,w);
		}
		Dinic();
		vis1[S]=vis2[T]=1;
		dfs1(S);dfs2(T);
		puts(cnt1+cnt2==n?"UNIQUE":"AMBIGUOUS");
	}
	return 0;
}
posted @ 2018-04-03 11:31  租酥雨  阅读(255)  评论(0)    收藏  举报