Zoj 2587 Unique Attack "Judging whether the minimum cut is unique"

Source: Internet
Author: User
Tags integer numbers

Unique Attack Time limit: 5 Seconds Memory Limit: 32768 KB

N supercomputers in the states of Antarctica is connected into a network. A network has a, simple topology:m different pairs of supercomputers is connected to each and an optical fibre. All connections was two-way, that's, they can be used in both directions. Data can is 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 both main computers of the network, so this there is no-to-transmit data from one's them to another. For each fibre the terrorists has calculated the sum of money they need to destroy the fibre. Of course, they want to minimize the cost of the operation, so it's required that's total sum spent for destroying the Fibres was minimal possible.

Now the leaders of the group wonder whether there are only one-to-do the selected operation. That's, they want to know if there am no, different sets of fibre connections that can being destroyed, such that the MA In supercomputers cannot connect to each other after it and the cost of the operation are minimal possible.


Input

The input file consists of several cases. The first line of the input file contains N, M, A and B (2 <= N <=, 1 <= M <= 10000, 1 <= A, A, <= N, A! = B), specifying the number of supercomputers in the network, the number of fibre connections, and the Nu Mbers 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 is given and the cost of destroying this connection. It is guaranteed this all costs be non-negative integer numbers not exceeding, no, and computers is directly connecte D by + than one fibre, no fibre connects a computer to itself and initially there is the the-to-transmit data from one Main supercomputer to another.


Output

If there is one-to-perform the operation, the output "UNIQUE" in a single line. In the other case output "ambiguous".


Sample Input

4 4 1 21 2 12 4 21 3 23 4 14 4 1 21 2 12 4 11 3 23 4 10 0 0 0

Sample Output

Uniqueambiguous

Author:Andrew Stankevich

Source: Andrew Stankevich ' s Contest #5


Test instructions: give you n points, m without edges and edges of weights, and give you the source point A and meeting point B. Ask you if the minimum cut of A to B is unique.



Minimum cut determination is unique:

1, first run the maximum flow, to obtain the residual quantity network;

2, in the residual network along the edge of the incomplete stream to find, statistics A can achieve the number of points sum1 and can reach the point of B sum2;

3, if sum1 + sum2 = N-2 is the smallest cut unique, and vice versa is not unique.



AC Code:


#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define MAXN 1010# Define MAXM 50000+10#define INF 0x3f3f3f3fusing namespace std;struct edge{int from, to, cap, flow, next;}; Edge Edge[maxm];int HEAD[MAXN], CUR[MAXN], edgenum;int dist[maxn];bool vis[maxn];int N, M, A, B;void init () {edgenum =    0; Memset (Head,-1, sizeof (head));}    void Addedge (int u, int v, int w) {Edge E1 = {u, V, W, 0, Head[u]};    Edge[edgenum] = E1;    Head[u] = edgenum++;    Edge E2 = {V, u, 0, 0, Head[v]};    Edge[edgenum] = E2; HEAD[V] = edgenum++;}    void Getmap () {int A, b, C;        while (m--) {scanf ("%d%d%d", &a, &b, &c);        Addedge (A, B, c);    Addedge (b, A, c);    }}bool BFS (int s, int t) {queue<int> Q;    memset (Dist,-1, sizeof (Dist));    Memset (Vis, false, sizeof (VIS));    Dist[s] = 0;    Vis[s] = true;    Q.push (s); while (!        Q.empty ()) {int u = q.front ();        Q.pop (); for (int i = head[u]; I ! =-1;            i = Edge[i].next) {Edge E = Edge[i];                if (!vis[e.to] && e.cap > E.flow) {dist[e.to] = Dist[u] + 1;                if (e.to = = t) return true;                Vis[e.to] = true;            Q.push (e.to); }}} return false;}    int DFS (int x, int a, int t) {if (x = = T | | a = = 0) return A;    int flow = 0, F;        for (int &i = cur[x]; i =-1; i = Edge[i].next) {Edge &e = Edge[i]; if (dist[e.to] = = Dist[x] + 1 && (f = DFS (e.to, Min (A, e.cap-e.flow), T)) > 0) {edge[i].flow            + = f;            Edge[i^1].flow-= f;            Flow + + F;            A-= f;        if (a = = 0) break; }} return flow;}        void Maxflow (int s, int t) {while (BFS (s, t)) {memcpy (cur, head, sizeof (head));    DFS (S, INF, T); }}int ans;//the number of points that can be reached by the super-source point + super sinks can be to the number of void sinksum (int u)//Calculate the source point can be to the number of {for (int i = head[u]; I! = 1; i = Edge[i].next)//forwardEdge {Edge E = Edge[i];        if (vis[e.to]) continue;            if (E.cap > E.flow) {ans++;            Vis[e.to] = true;        Sinksum (e.to);        }}}void sourcesum (int u)//calculates the number of points to sink point {for (int i = head[u]; I! = 1; i = Edge[i].next) {Edge E = Edge[i];        if (vis[e.to]) continue;            if (edge[i^1].cap-edge[i^1].flow)//See reverse arc {ans++;            Vis[e.to] = true;        Sourcesum (e.to);    }}}void Solve () {ans = 0;    Memset (Vis, false, sizeof (VIS));    Vis[a] = vis[b] = true;    Sinksum (A);    Sourcesum (B); if (ans = = N-2) printf ("unique\n"),//Minimum cut unique else printf ("ambiguous\n");//Not unique}int main () {while (scanf (" %d%d%d%d ", &n, &m, &a, &b), n| | m| | a| |        B) {init ();        Getmap (); Maxflow (A, B);//Run Once the maximum flow solve ();//Determine if the minimum cut is unique} return 0;



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Zoj 2587 Unique Attack "Judging whether the minimum cut is unique"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.