A - Unique Attack
Problem 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 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
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
Sample Output
UNIQUE AMBIGUOUS
解题:最小割的唯一性判定
利用残量网络,除源汇点外的点,要么可以沿着未满流的弧从源点到达,要么可以到大汇。则唯一,如果存在点,既不能由源点到达,又不去由该点到达汇,则不唯一
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define INF 0x3f3f3f3f 15 using namespace std; 16 const int maxn = 810; 17 struct arc { 18 int to,flow,next; 19 arc(int x = 0,int y = 0,int z = -1) { 20 to = x; 21 flow = y; 22 next = z; 23 } 24 }; 25 arc e[maxn*maxn]; 26 int head[maxn],d[maxn],cur[maxn]; 27 int n,m,S,T,tot,src,sink; 28 void add(int u,int v,int flow) { 29 e[tot] = arc(v,flow,head[u]); 30 head[u] = tot++; 31 e[tot] = arc(u,0,head[v]); 32 head[v] = tot++; 33 } 34 bool bfs() { 35 queue<int>q; 36 memset(d,-1,sizeof(d)); 37 d[S] = 1; 38 q.push(S); 39 while(!q.empty()) { 40 int u = q.front(); 41 q.pop(); 42 for(int i = head[u]; ~i; i = e[i].next) { 43 if(e[i].flow && d[e[i].to] == -1) { 44 d[e[i].to] = d[u] + 1; 45 q.push(e[i].to); 46 } 47 } 48 } 49 return d[T] > -1; 50 } 51 int dfs(int u,int low) { 52 if(u == T) return low; 53 int tmp = 0,a; 54 for(int &i = cur[u]; ~i; i = e[i].next) { 55 if(e[i].flow && d[e[i].to] == d[u] + 1 && (a=dfs(e[i].to,min(low,e[i].flow)))) { 56 tmp += a; 57 low -= a; 58 e[i].flow -= a; 59 e[i^1].flow += a; 60 if(!low) break; 61 } 62 } 63 if(tmp == 0) d[u] = -1; 64 return tmp; 65 } 66 int dinic() { 67 int tmp = 0; 68 while(bfs()) { 69 memcpy(cur,head,sizeof(head)); 70 tmp += dfs(S,INF); 71 } 72 return tmp; 73 } 74 bool vis[maxn]; 75 void dfs1(int u) { 76 for(int i = head[u]; ~i; i = e[i].next) { 77 if(!vis[e[i].to] && e[i].flow) { 78 vis[e[i].to] = true; 79 dfs1(e[i].to); 80 } 81 } 82 } 83 void dfs2(int u) { 84 for(int i = head[u]; ~i; i = e[i].next) { 85 if(!vis[e[i].to] && e[i^1].flow) { 86 vis[e[i].to] = true; 87 dfs2(e[i].to); 88 } 89 } 90 } 91 int main() { 92 int u,v,w; 93 while(~scanf("%d %d %d %d",&n,&m,&S,&T)) { 94 memset(head,-1,sizeof(head)); 95 for(int i = tot = 0; i < m; i++) { 96 scanf("%d %d %d",&u,&v,&w); 97 add(u,v,w); 98 add(v,u,w); 99 } 100 dinic(); 101 //cout<<dinic()<<endl; 102 memset(vis,false,sizeof(vis)); 103 vis[S] = vis[T] = true; 104 dfs1(S); 105 dfs2(T); 106 bool flag = true; 107 for(int i = 1; i <= n; i++) 108 if(!vis[i]) { 109 flag = false; 110 break; 111 } 112 flag?puts("UNIQUE"):puts("AMBIGUOUS"); 113 } 114 return 0; 115 }