題意還行,就是問最小割是不是唯一的。
假設源點為s,匯點為t,最小割將點集分為兩個點集S,T
那麼S的一種情況就是,從源點出發,dfs殘留網絡,沿著不滿流的邊走,能走到的所有點都屬於S,全集減去S即為T。
T的一種情況是,如果一個點沿著一些不滿流的邊能到達匯點,那麼該點屬於T。
而本題要求的是唯一性,觀察網絡有這樣一個特性,一個點如果既不能由S沿著不滿流的邊到達,也不能沿著一些不滿流的邊到達T,那麼該點既可以屬於S,也可屬於T,那麼最小割不是唯一的。
為什麼呢? 如果一個點v不能由S沿著不滿流的邊到達,必然是S到v的所有邊都滿流,同樣如果v沿著不滿流的邊到不了T,v到T的所有邊也都是滿流,並且,S到v的流量等於v到T的流量,否則兩邊流量不對等會導致不滿流的邊出現,此時既然流量相等,那麼v屬於S或者T都是可以的,也就是說最小割不唯一
這裡搜索要搜兩次,一次是從源點出發,另一次是從匯點出發搜索逆圖
[cpp]
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define eps 1e-5
#define MAXN 1111
#define MAXM 55555
#define INF 10000007
using namespace std;
struct node
{
int u, v; // vtex
int c; // cacity
int f; // current f in this arc
int next, r;
} edge[MAXM];
int dist[MAXN], nm[MAXN], src, des, n;
int head[MAXN], e;
void add(int x, int y, int c)
{
edge[e].u = x;
edge[e].v = y;
edge[e].c = c;
edge[e].f = 0;
edge[e].r = e + 1;
edge[e].next = head[x];
head[x] = e++;
edge[e].u = y;
edge[e].v = x;
edge[e].c = 0;
edge[e].f = 0;
edge[e].r = e - 1;
edge[e].next = head[y];
head[y] = e++;
}
void rev_BFS()
{
int Q[MAXN], h = 0, t = 0;
for(int i = 1; i <= n; ++i)
{
dist[i] = MAXN;
nm[i] = 0;
}
Q[t++] = des;
dist[des] = 0;
nm[0] = 1;
while(h != t)
{
int v = Q[h++];
for(int i = head[v]; i != -1; i = edge[i].next)
{
if(edge[edge[i].r].c == 0 || dist[edge[i].v] < MAXN)continue;
dist[edge[i].v] = dist[v] + 1;
++nm[dist[edge[i].v]];
Q[t++] = edge[i].v;
}
}
}
void init()
{
e = 0;
memset(head, -1, sizeof(head));
}
int maxflow()
{
rev_BFS();
int u;
int total = 0;
int cur[MAXN], rpath[MAXN];
for(int i = 1; i <= n; ++i)cur[i] = head[i];
u = src;
while(dist[src] < n)
{
if(u == des) // find an augmenting path
{
int tf = INF;
for(int i = src; i != des; i = edge[cur[i]].v)
tf = min(tf, edge[cur[i]].c);
for(int i = src; i != des; i = edge[cur[i]].v)
{
edge[cur[i]].c -= tf;
edge[edge[cur[i]].r].c += tf;
edge[cur[i]].f += tf;
edge[edge[cur[i]].r].f -= tf;
}
total += tf;
u = src;
}
int i;
for(i = cur[u]; i != -1; i = edge[i].next)
if(edge[i].c > 0 && dist[u] == dist[edge[i].v] + 1)break;
if(i != -1) // find an admissible arc, then Advance
{
cur[u] = i;
rpath[edge[i].v] = edge[i].r;
u = edge[i].v;
}
else // no admissible arc, then relabel this vtex
{
if(0 == (--nm[dist[u]]))break; // GAP cut, Important!
cur[u] = head[u];
int mindist = n;
for(int j = head[u]; j != -1; j = edge[j].next)
if(edge[j].c > 0)mindist = min(mindist, dist[edge[j].v]);
dist[u] = mindist + 1;
++nm[dist[u]];
if(u != src)
u = edge[rpath[u]].v; // Backtrack
}
}
return total;
}
int nt, m;
int vis[MAXN];
vector<int>g[MAXN];
void dfs1(int u)
{
vis[u] = 1;
for(int i = head[u]; i != -1; i = edge[i].next)
if(!vis[edge[i].v] && edge[i].c)
dfs1(edge[i].v);
}
void dfs2(int u)
{
vis[u] = 1;
int size = g[u].size();
for(int i = 0; i < size; i++)
if(!vis[g[u][i]]) dfs2(g[u][i]);
}
int main()
{
int u, v, w;
while(scanf("%d%d%d%d", &nt, &m, &src, &des) != EOF)
{
init();
if(nt == 0 && m == 0 && src == 0 && des == 0) break;
n = nt;
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
maxflow();
memset(vis, 0, sizeof(vis));
for(int i = 0; i < MAXN; i++) g[i].clear();
for(int i = 0; i < e; i += 2)
if(edge[i].c) g[edge[i].v].push_back(edge[i].u);
dfs1(src);
dfs2(des);
int flag = 1;
for(int i = 1; i <= nt; i++)
if(!vis[i]) flag = 0;
if(!flag) printf("AMBIGUOUS\n");
else printf("UNIQUE\n");
}
return 0;
}