Unique Attack (zoj 2587 判定最小割是否唯一)

46 篇文章 0 订阅

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 asimple 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 fromone 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 computersof the network, so that there is no way to transmit data from one of them to another. For each fibrethe terrorists have calculated the sum of money they need to destroy the fibre. Of course, they want tominimize the cost of the operation, so it is required that the total sum spent for destroying the fibres wasminimal 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 thatthe main supercomputers cannot connect to each other after it and the cost of the operation is minimalpossible.


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, andthe 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 connectsare given and the cost of destroying this connection. It is guaranteed that all costs are non-negativeinteger 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 mainsupercomputer 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


题意:判断最小割是否唯一。

参考点击打开链接

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1005;
const int MAXN = 2005;
const int MAXM = 200010;
const int N = 1005;

struct Edge
{
    int to,next,cap,flow;
}edge[MAXM];

int n,m,a,b,cnt1,cnt2;
bool vis[MAXN];
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];

void init()
{
    cnt1=0;cnt2=0;
    tol=0;
    memset(head,-1,sizeof(head));
}

//加边,单向图三个参数,双向图四个参数
void addedge(int u,int v,int w,int rw=0)
{
    edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u];
    edge[tol].flow=0; head[u]=tol++;
    edge[tol].to=u; edge[tol].cap=rw; edge[tol].next=head[v];
    edge[tol].flow=0; head[v]=tol++;
}

//输入参数:起点,终点,点的总数
//点的编号没有影响,只要输入点的总数
int sap(int start,int end,int N)
{
    memset(gap,0,sizeof(gap));
    memset(dep,0,sizeof(dep));
    memcpy(cur,head,sizeof(head));
    int u=start;
    pre[u]=-1;
    gap[0]=N;
    int ans=0;
    while (dep[start]<N)
    {
        if (u==end)
        {
            int Min=INF;
            for (int i=pre[u];i!=-1;i=pre[edge[i^1].to])
                if (Min>edge[i].cap-edge[i].flow)
                    Min=edge[i].cap-edge[i].flow;
            for (int i=pre[u];i!=-1;i=pre[edge[i^1].to])
            {
                edge[i].flow+=Min;
                edge[i^1].flow-=Min;
            }
            u=start;
            ans+=Min;
            continue;
        }
        bool flag=false;
        int v;
        for (int i=cur[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].to;
            if (edge[i].cap-edge[i].flow && dep[v]+1==dep[u])
            {
                flag=true;
                cur[u]=pre[v]=i;
                break;
            }
        }
        if (flag)
        {
            u=v;
            continue;
        }
        int Min=N;
        for (int i=head[u];i!=-1;i=edge[i].next)
            if (edge[i].cap-edge[i].flow && dep[edge[i].to]<Min)
            {
                Min=dep[edge[i].to];
                cur[u]=i;
            }
        gap[dep[u]]--;
        if (!gap[dep[u]]) return ans;
        dep[u]=Min+1;
        gap[dep[u]]++;
        if (u!=start) u=edge[pre[u]^1].to;
    }
    return ans;
}

void dfs1(int u)
{
    if (vis[u]) return ;
    vis[u]=true;
    cnt1++;
    for (int i=head[u];~i;i=edge[i].next)
        if (edge[i].cap-edge[i].flow>0&&!vis[edge[i].to])
            dfs1(edge[i].to);
}

void dfs2(int u)
{
    if (vis[u]) return ;
    vis[u]=true;
    cnt2++;
    for (int i=head[u];~i;i=edge[i].next)
        if (edge[i^1].cap-edge[i^1].flow>0&&!vis[edge[i].to])
            dfs2(edge[i].to);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
    int i,j,u,v,w;
    while (~scanf("%d%d%d%d",&n,&m,&a,&b))
    {
        if (n==0&&m==0&&a==0&&b==0) break;
        init();
        for (i=0;i<m;i++)
        {
            sfff(u,v,w);
            addedge(u,v,w,w);
        }
        int ans=sap(a,b,n);
        mem(vis,false);
        dfs1(a);
        mem(vis,false);
        dfs2(b);
        if (cnt1+cnt2==n)
            pf("UNIQUE\n");
        else
            pf("AMBIGUOUS\n");
    }
    return 0;
}




  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值