题意

判断一个无向图的割是否唯一

思路

错误思路:一开始想的是判断割边是否都是关键割边,那既然割边两端点能连通S、T点的边是关键边,那么只要遇到有某个边两端点不连通S or T则这条边就不是关键割边(当然要把不是割边的满流边筛掉)。这种主观臆断的naive想法是不行的,因为那个判断关键割边的条件只是个充分条件,我们没法证明它是个充要条件。

正确思路:求完最大流后在残留网络中从S点开始dfs遍历,再把残留网络反向,然后从T点开始dfs遍历,如果能遍历到所有点则最小割唯一

代码

 
#include 
#include 
#include 
#include 
#include 
#include 
#define MID(x,y) ((x+y)/2)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int MAXV = 1005;
const int MAXE = 30005;
const int oo = 0x3fffffff;
struct node{
    int u, v, flow;
    int opp;
    int next;
};
struct Dinic{
    node arc[MAXE];
    int vn, en, head[MAXV];     //vn点个数(包括源点汇点),en边个数
    int cur[MAXV];              //当前弧
    int q[MAXV];                //bfs建层次图时的队列
    int path[MAXE], top;        //存dfs当前最短路径的栈
    int dep[MAXV];              //各节点层次
    void init(int n){
        vn = n;
        en = 0;
        mem(head, -1);
    }
    void insert_flow(int u, int v, int flow){
        arc[en].u = u;
        arc[en].v = v;
        arc[en].flow = flow;
        arc[en].opp = en + 1;
        arc[en].next = head[u];
        head[u] = en ++;

        arc[en].u = v;
        arc[en].v = u;
        arc[en].flow = flow;       //反向弧
        arc[en].opp = en - 1;
        arc[en].next = head[v];
        head[v] = en ++;
    }
    bool bfs(int s, int t){
        mem(dep, -1);
        int lq = 0, rq = 1;
        dep[s] = 0;
        q[lq] = s;
        while(lq < rq){
            int u = q[lq ++];
            if (u == t){
                return true;
            }
            for (int i = head[u]; i != -1; i = arc[i].next){
                int v = arc[i].v;
                if (dep[v] == -1 && arc[i].flow > 0){
                    dep[v] = dep[u] + 1;
                    q[rq ++] = v;
                }
            }
        }
        return false;
    }
    int solve(int s, int t){
        int maxflow = 0;
        while(bfs(s, t)){
            int i, j;
            for (i = 1; i <= vn; i ++)  cur[i] = head[i];
            for (i = s, top = 0;;){
                if (i == t){
                    int mink;
                    int minflow = 0x3fffffff;
                    for (int k = 0; k < top; k ++)
                        if (minflow > arc[path[k]].flow){
                            minflow = arc[path[k]].flow;
                            mink = k;
                        }
                    for (int k = 0; k < top; k ++)
                        arc[path[k]].flow -= minflow, arc[arc[path[k]].opp].flow += minflow;
                    maxflow += minflow;
                    top = mink;		//arc[mink]这条边流量变为0, 则直接回溯到该边的起点即可(这条边将不再包含在增广路内).
                    i = arc[path[top]].u;
                }
                for (j = cur[i]; j != -1; cur[i] = j = arc[j].next){
                    int v = arc[j].v;
                    if (arc[j].flow && dep[v] == dep[i] + 1)
                        break;
                }
                if (j != -1){
                    path[top ++] = j;
                    i = arc[j].v;
                }
                else{
                    if (top == 0)   break;
                    dep[i] = -1;
                    i = arc[path[-- top]].u;
                }
            }
        }
        return maxflow;
    }
}dinic;
bool vis[MAXV];
int num;
void reach(int u){
    vis[u] = 1;
    num ++;
    for (int i = dinic.head[u]; i != -1; i = dinic.arc[i].next){
        int v = dinic.arc[i].v;
        if (vis[v] || dinic.arc[i].flow <= 0) continue;
        reach(v);
    }
    return ;
}
void work(int n, int A, int B){
    num = 0;
    mem(vis, 0);
    reach(A);
    for (int i = 0; i < dinic.en; i += 2){
        swap(dinic.arc[i].flow, dinic.arc[i^1].flow);
    }
    reach(B);
    if (num == n){
        puts("UNIQUE");
    }
    else{
        puts("AMBIGUOUS");
    }
}
int main(){
	//freopen("test.in", "r", stdin);
	//freopen("test.out", "w", stdout);
    int n, m, A, B;
    while(scanf("%d %d %d %d", &n, &m, &A, &B), n){
        dinic.init(n);
        for (int i = 0; i < m; i ++){
            int u,v,w;
            scanf("%d %d %d", &u, &v, &w);
            dinic.insert_flow(u, v, w);
        }
        dinic.solve(A, B);
        work(n, A, B);
    }
	return 0;
}
举杯独醉,饮罢飞雪,茫然又一年岁。 ------AbandonZHANG