Sabotage UVA - 10480 (输出割边)

时间:2021-03-13 04:24:55

题意:。。。。emm。。。就是一个最小割最大流,。,。。。用dinic跑一遍。。

然后让你输出割边,就是 u为能从起点到达的点,  v为不能从起点到达的点

最后在残余路径中用dfs跑一遍  能到达的路标记一下  

然后循环判断输出即可  还有不要忘了是正向路  所以循环时i+=2

 

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define maxn 100009
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int INF = 0x3f3f3f3f;
int head[maxn], d[maxn], cur[maxn], vis[maxn];
int n, m, s, t;
int cnt = 0;
struct node{
    int u, v, c, next;
}Node[maxn*2];

void add_(int u, int v, int c)
{
    Node[cnt].u = u;
    Node[cnt].v = v;
    Node[cnt].c = c;
    Node[cnt].next = head[u];
    head[u] = cnt++;
}

void add(int u, int v, int c)
{
    add_(u, v, c);
    add_(v, u, c);
}

bool bfs()
{
    queue<int> Q;
    mem(d,0);
    Q.push(s);
    d[s] = 1;
    while(!Q.empty())
    {
        int u = Q.front(); Q.pop();
        for(int i=head[u]; i!=-1; i=Node[i].next)
        {
            node e = Node[i];
            if(!d[e.v] && e.c > 0)
            {
                d[e.v] = d[e.u] + 1;
                Q.push(e.v);
                if(e.v == t) return 1;
            }
        }
    }
    return d[t] != 0;
}

int dfs(int u, int cap)
{
    if(u == t || cap == 0)
        return cap;
    int ret = 0;
    for(int &i=cur[u]; i!=-1; i=Node[i].next)
    {
        node e = Node[i];
        if(d[e.v] == d[e.u] + 1 && e.c > 0)
        {
            int V = dfs(e.v, min(cap, e.c));
            Node[i].c -= V;
            Node[i^1].c += V;
            cap -= V;
            ret += V;
            if(cap == 0) break;
        }
    }
    return ret;
}

int dinic()
{
    int ans = 0;
    while(bfs())
    {
        memcpy(cur, head, sizeof(head));
        ans += dfs(s, INF);
    }
    return ans;
}

void find(int u)
{
    for(int i=head[u]; i!=-1; i=Node[i].next)
    {
        node e = Node[i];
        if(vis[e.v] || e.c == 0) continue;
        vis[e.v] = 1;
        find(e.v);
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m) && n+m)
    {
        mem(vis,0);
        mem(head,-1);
        cnt = 0;
        s = 1; t = 2;
        for(int i=0; i<m; i++)
        {
            int u, v, c;
            scanf("%d%d%d",&u,&v,&c);
            add(u, v, c);
        }

        dinic();
        vis[s] = 1;
        find(s);       //寻找能从s到达的路  
        for(int i=0; i<cnt; i+=2)
            if(vis[Node[i].u] && !vis[Node[i].v] || !vis[Node[i].u] && vis[Node[i].v])   // 如果一个能到达 另一个不能到达 则为割边 输出即可
                cout<< Node[i].u << " " << Node[i].v <<endl;
        cout<< endl;
    }

    return 0;
}