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
 
题意:给出一个无向图,每条边都有一个花费,给出s和t,要破坏一些边,使得s和t不连通,并且破坏的边的总花费要最小。问是否存在多种破坏边的方案。
思路:转化为求原图的最小割是否唯一。求完最大流后,以未满流的边分别建正图和反图,分别以s点和t点开始dfs,dfs结束后,若有点没有访问过,那么这个点不属于S和T集合,那么这些点构成的边只要任意割一条就好了,所以若有点没有访问过,则最小割不唯一。
 
AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>
#include <vector>
#include <algorithm>
#define ll long long
#define L(rt) (rt<<1)
#define R(rt)  (rt<<1|1)
using namespace std;

const int INF = 1e9;
const int maxn = 1005;

struct Edge{
    int u, v, cap, flow, next;
}et[maxn * maxn];
int low[maxn], cnt[maxn], dis[maxn], pre[maxn], cur[maxn], eh[maxn], col[maxn];
int n, m, s, t, num;
vector<int> G[maxn], RG[maxn];
void init(){
    memset(eh, -1, sizeof(eh));
    memset(col, 0, sizeof(col));
    for(int i = 0; i <= n; i++)
    {
        G[i].clear();
        RG[i].clear();
    }
    num = 0;
}
void add(int u, int v, int cap, int flow){
    Edge e = {u, v, cap, flow, eh[u]};
    et[num] = e;
    eh[u] = num++;
}
void addedge(int u, int v, int cap){
    add(u, v, cap, 0);
    add(v, u, 0, 0);
}
int isap(int s, int t, int nv){
    int u, v, now, flow = 0;
    memset(low, 0, sizeof(low));
    memset(cnt, 0, sizeof(cnt));
    memset(dis, 0, sizeof(dis));
    for(u = 0; u <= nv; u++) cur[u] = eh[u];
    low[s] = INF, cnt[0] = nv, u = s;
    while(dis[s] < nv)
    {
        for(now = cur[u]; now != -1; now = et[now].next)
        if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break;
        if(now != -1)
        {
            cur[u] = pre[v] = now;
            low[v] = min(low[u], et[now].cap - et[now].flow);
            u = v;
            if(u == t)
            {
                for(; u != s; u = et[pre[u]].u)
                {
                    et[pre[u]].flow += low[t];
                    et[pre[u]^1].flow -= low[t];
                }
                flow += low[t];
                low[s] = INF;
            }
        }
        else
        {
            if(--cnt[dis[u]] == 0) break;
            dis[u] = nv, cur[u] = eh[u];
            for(now = eh[u]; now != -1; now = et[now].next)
            if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1)
            dis[u] = dis[et[now].v] + 1;
            cnt[dis[u]]++;
            if(u != s) u = et[pre[u]].u;
        }
    }
    return flow;
}
void dfs1(int u){
    col[u] = 1;
    for(int i = 0; i < (int)G[u].size(); i++)
    {
        int v = G[u][i];
        if(!col[v]) dfs1(v);
    }
}
void dfs2(int u){
    col[u] = 2;
    for(int i = 0; i < (int)RG[u].size(); i++)
    {
        int v = RG[u][i];
        if(!col[v]) dfs2(v);
    }
}
bool judge(){
    dfs1(s);
    dfs2(t);
    for(int i = 1; i <= n; i++)
    if(!col[i]) return false;
    return true;
}
int main()
{
    int a, b, c;
    while(scanf("%d%d%d%d", &n, &m, &s, &t), n || m || s || t)
    {
        init();
        while(m--)
        {
            scanf("%d%d%d", &a, &b, &c);
            addedge(a, b, c);
            addedge(b, a, c);
        }
        isap(s, t, n + 1);
        for(int i = 0; i < num; i += 2)
        if(et[i].cap - et[i].flow)
        {
            int u = et[i].u, v = et[i].v;
            G[u].push_back(v);
            RG[v].push_back(u);
        }
        if(judge()) printf("UNIQUE\n");
        else printf("AMBIGUOUS\n");
    }
    return 0;
}

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