ZOJ 2587 Unique Attack 判斷最小割是否唯一

 
Unique Attack

Time Limit: 5 Seconds Memory Limit: 32768 KB

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



Author: Andrew Stankevich
Source: Andrew Stankevich's Contest #5

解法1:

求一次最大流,對每一個割邊,將其權值改爲inf,再做一次最大流,如果等於初始的流值,說明最小割不唯一。

用時1150 ms

 

解法2:

 先做一次最大流,最大流之後圖分成了兩部分,然後從分別從源點和匯點進行dfs

標記遍歷的點,看圖中有沒有點不能被搜到 ,都能被搜到則最小割唯一。

 

思路(轉):判斷最小割是否唯一,先求一次最大流,然後在殘留網絡中分別從源匯開始dfs一次,找出最小割[S,T],如果SUT不包含所有點,那麼最小割不唯一。假設點i不被SUT包含,那麼殘留網絡中s不能到達i,i不能到達t,即進入i的邊和從i出去的邊都滿流,假設某條進入i的邊x滿流,這些流量從若干條邊y流出i,那麼,如果選x爲割邊,或者選所有對應的y爲割邊,不會影響最大流,即最小割容量不變,最小割也就不唯一。

用時130 ms

 

代碼1:

#include<cstdio>
#include<cstring>
#define N 1005
#define M 20005
#define inf 999999999
#define min(a,b) ((a)<(b)?(a):(b))

int n,m,s,t,num,adj[N],dis[N],q[N],f[N],f1[N];
struct edge
{
	int v,w,c,pre;
}e[M];
void insert(int u,int v,int w)
{
	e[num]=(edge){v,w,w,adj[u]};
	adj[u]=num++;
	e[num]=(edge){u,w,w,adj[v]};
	adj[v]=num++;
}
int bfs()
{
	int i,x,v,head=0,tail=0;
	memset(dis,0,sizeof(dis));
	dis[s]=1;
	q[tail++]=s;
	while(head<tail)
	{
		x=q[head++];
		for(i=adj[x];~i;i=e[i].pre)
			if(e[i].w&&!dis[v=e[i].v])
			{
				dis[v]=dis[x]+1;
				if(v==t)
					return 1;
				q[tail++]=v;
			}
	}
	return 0;
}
int dfs(int x,int limit)
{
	if(x==t)
		return limit;
	int i,v,tmp,cost=0;
	for(i=adj[x];~i&&cost<limit;i=e[i].pre)
		if(e[i].w&&dis[x]==dis[v=e[i].v]-1)
		{
			tmp=dfs(v,min(limit-cost,e[i].w));
			if(tmp)
			{
				e[i].w-=tmp;
				e[i^1].w+=tmp;
				cost+=tmp;
			}
			else
				dis[v]=-1;
		}
	return cost;
}
int Dinic()
{
	int ans=0;
	while(bfs())
		ans+=dfs(s,inf);
	return ans;
}
int cnt1,cnt2;
void dfs1(int x)
{
	f[x]=1;
	cnt1++;
	for(int i=adj[x];~i;i=e[i].pre)
		if(!f[e[i].v]&&e[i].w)
			dfs1(e[i].v);
}
void dfs2(int x)
{
	f1[x]=1;
	cnt2++;
	for(int i=adj[x];~i;i=e[i].pre)
		if(!f1[e[i].v]&&e[i^1].w)
			dfs2(e[i].v);
}
int main()
{
	while(~scanf("%d%d%d%d",&n,&m,&s,&t),n)
	{
		int i,j,k;
		num=0;
		memset(adj,-1,sizeof(adj));
		while(m--)
		{
			scanf("%d%d%d",&i,&j,&k);
			insert(i,j,k);
		}
		Dinic();
		memset(f,0,sizeof(f));
		memset(f1,0,sizeof(f1));
		cnt1=cnt2=0;
		dfs1(s);
		dfs2(t);
		puts(cnt1+cnt2==n?"UNIQUE":"AMBIGUOUS");
	}
}

 


代碼2:

#include<cstdio>
#include<cstring>
#define N 1005
#define M 20005
#define inf 999999999
#define min(a,b) ((a)<(b)?(a):(b))

int n,m,s,t,num,adj[N],dis[N],q[N],f[N],f1[N];
struct edge
{
	int v,w,c,pre;
}e[M];
void insert(int u,int v,int w)
{
	e[num]=(edge){v,w,w,adj[u]};
	adj[u]=num++;
	e[num]=(edge){u,w,w,adj[v]};
	adj[v]=num++;
}
int bfs()
{
	int i,x,v,head=0,tail=0;
	memset(dis,0,sizeof(dis));
	dis[s]=1;
	q[tail++]=s;
	while(head<tail)
	{
		x=q[head++];
		for(i=adj[x];~i;i=e[i].pre)
			if(e[i].w&&!dis[v=e[i].v])
			{
				dis[v]=dis[x]+1;
				if(v==t)
					return 1;
				q[tail++]=v;
			}
	}
	return 0;
}
int dfs(int x,int limit)
{
	if(x==t)
		return limit;
	int i,v,tmp,cost=0;
	for(i=adj[x];~i&&cost<limit;i=e[i].pre)
		if(e[i].w&&dis[x]==dis[v=e[i].v]-1)
		{
			tmp=dfs(v,min(limit-cost,e[i].w));
			if(tmp)
			{
				e[i].w-=tmp;
				e[i^1].w+=tmp;
				cost+=tmp;
			}
			else
				dis[v]=-1;
		}
	return cost;
}
int Dinic()
{
	int ans=0;
	while(bfs())
		ans+=dfs(s,inf);
	return ans;
}
int cnt1,cnt2;
void dfs1(int x)
{
	f[x]=1;
	cnt1++;
	for(int i=adj[x];~i;i=e[i].pre)
		if(!f[e[i].v]&&e[i].w)
			dfs1(e[i].v);
}
void dfs2(int x)
{
	f1[x]=1;
	cnt2++;
	for(int i=adj[x];~i;i=e[i].pre)
		if(!f1[e[i].v]&&e[i^1].w)
			dfs2(e[i].v);
}
int main()
{
	while(~scanf("%d%d%d%d",&n,&m,&s,&t),n)
	{
		int i,j,k;
		num=0;
		memset(adj,-1,sizeof(adj));
		while(m--)
		{
			scanf("%d%d%d",&i,&j,&k);
			insert(i,j,k);
		}
		Dinic();
		memset(f,0,sizeof(f));
		memset(f1,0,sizeof(f1));
		cnt1=cnt2=0;
		dfs1(s);
		dfs2(t);
		puts(cnt1+cnt2==n?"UNIQUE":"AMBIGUOUS");
	}
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章