Unique Attack
Description
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
算法: 根据最小割最大流的性质。可以先进行一次最小割。求完最小割后就会存在割集[S,T].这时候分别从S和T发出判断割集是否唯一就可以了。
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF = 1 << 20;
const int MAXN = 800 + 10;
struct Edge{
int from,to,cap,flow;
Edge(){};
Edge(int _from,int _to,int _cap,int _flow)
:from(_from),to(_to),cap(_cap),flow(_flow){};
};
vector<Edge> edges;
vector<int> G[MAXN];
int N,M,src,sink;
int d[MAXN],cur[MAXN];
struct MinCut{
void init(int n) {
for(int i = 0;i <= n+5;++i) G[i].clear();
edges.clear();
}
void addEdge(int from,int to,int cap) {
edges.push_back(Edge(from,to,cap,0));
edges.push_back(Edge(to,from,0,0));
int sz = edges.size();
G[from].push_back(sz - 2);
G[to].push_back(sz - 1);
}
bool BFS() {
queue<int> Q;
memset(d,-1,sizeof(d));
Q.push(src);
d[src] = 0;
while(!Q.empty()) {
int x = Q.front(); Q.pop();
for(int i = 0;i < (int)G[x].size();++i) {
Edge& e = edges[G[x][i]];
if(d[e.to] == -1 && e.cap > e.flow) {
d[e.to] = d[x] + 1;
Q.push(e.to);
}
}
}
return d[sink] > 0;
}
int DFS(int x,int a) {
if(x == sink || a == 0)
return a;
int flow = 0,f;
for(int& i = cur[x];i < (int)G[x].size();++i) {
Edge& e = edges[G[x][i]];
if(d[e.to] == d[x] + 1 && (f = DFS(e.to,min(a,e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}
int maxFlow(int s,int t) {
src = s, sink = t;
int flow = 0;
while(BFS()) {
memset(cur,0,sizeof(cur));
flow += DFS(src,INF);
}
return flow;
}
};
/*
1、建图完成后求一个最小割。
2、求完之后,利用残余网络从源点和汇点进行遍历。
3、如果两次遍历后存在点没有被遍历到,那么最小割不唯一,否则唯一。
*/
struct Query{ //判断最小割是否唯一性
int cnt;
bool vst[MAXN];
void dfs1(int u) {
++cnt;
vst[u] = 1;
for(int i = 0;i < (int)G[u].size();++i) {
Edge& e = edges[G[u][i]];
if(!vst[e.to] && e.cap > e.flow) //没有满流的边
dfs1(e.to);
}
}
void dfs2(int u) {
++cnt;
vst[u] = 1;
for(int i = 0;i < (int)G[u].size();++i) {
int to = edges[G[u][i]].to; ///!!!!!!
Edge& e = edges[G[u][i]^1]; //注意是反向边的容量!!!!
if(!vst[to] && e.cap > e.flow) //没有满流的边
dfs2(to);
}
}
bool check(int s,int t,int n) {
cnt = 0;
memset(vst,0,sizeof(vst));
dfs1(s);
memset(vst,0,sizeof(vst));
dfs2(t);
return cnt == n;
}
};
MinCut mc;
Query query;
void debug() {
for(int i = 0;i < (int)G[2].size();++i) {
Edge& e = edges[G[2][i]];
printf("to: %d ----> %d %d\n",e.to,e.cap,e.flow);
}
}
int main()
{
while(~scanf("%d %d %d %d",&N,&M,&src,&sink)) {
if(N == 0 && M == 0 && src == 0 && sink == 0) break;
mc.init(N);
int x,y,c;
for(int i = 0;i < M;++i) {
scanf("%d %d %d",&x,&y,&c);
mc.addEdge(x,y,c);
mc.addEdge(y,x,c);
}
mc.maxFlow(src,sink);
if(query.check(src,sink,N)) puts("UNIQUE");
else puts("AMBIGUOUS");
}
}
/*
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
*/