zoj 2587 Unique Attack 最小割判定

时间:2022-03-30 19:09:03

题目链接

让你判断最小割是否唯一。

判断方法是, 先求一遍最大流, 然后从源点dfs一次, 搜索未饱和边的数目。 从汇点dfs一次, 同样也是搜索未饱和边的数目, 看总和是否等于n。 如果等于n那么唯一。

具体可以看这里, http://www.cnblogs.com/Lyush/archive/2013/05/01/3053640.html

 #include<bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))
const int inf = 1e8;
const int maxn = 2e4+;
int head[maxn*], s, t, num, q[maxn*], dis[], vis[], cnt;
struct node
{
int to, nextt, c;
}e[maxn*];
void init() {
mem1(head);
num = cnt = ;
mem(vis);
}
void add(int u, int v, int c) {
e[num].to = v; e[num].nextt = head[u]; e[num].c = c; head[u] = num++;
e[num].to = u; e[num].nextt = head[v]; e[num].c = c; head[v] = num++;
}
int bfs() {
int u, v, st = , ed = ;
mem(dis);
dis[s] = ;
q[ed++] = s;
while(st<ed) {
u = q[st++];
for(int i = head[u]; ~i; i = e[i].nextt) {
v = e[i].to;
if(e[i].c&&!dis[v]) {
dis[v] = dis[u]+;
if(v == t)
return ;
q[ed++] = v;
}
}
}
return ;
}
int dfs(int u, int limit) {
if(u == t)
return limit;
int cost = ;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(e[i].c&&dis[u] == dis[v]-) {
int tmp = dfs(v, min(limit-cost, e[i].c));
if(tmp>) {
e[i].c -= tmp;
e[i^].c += tmp;
cost += tmp;
if(cost == limit)
break;
} else {
dis[v] = -;
}
}
}
return cost;
}
int dinic() {
int ans = ;
while(bfs()) {
ans += dfs(s, inf);
}
return ans;
}
void dfs(int u) {
vis[u] = ;
cnt++;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!vis[v]&&e[i].c) {
dfs(v);
}
}
}
void dfs1(int u) {
vis[u] = ;
cnt++;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!vis[v]&&e[i^].c) {
dfs1(v);
}
}
}
int main()
{
int n, m, x, y, z;
while(scanf("%d%d%d%d", &n, &m, &s, &t)) {
if(n+m+s+t==)
break;
init();
while(m--) {
scanf("%d%d%d", &x, &y, &z);
add(x, y, z);
}
int ans = dinic();
dfs(s);
dfs1(t);
if(cnt == n) {
cout<<"UNIQUE"<<endl;
} else {
cout<<"AMBIGUOUS"<<endl;
}
}
}