无向图求最少删除多少点使得剩下的图不连通 网络流最小割+拆点

  • Post author:
  • Post category:其他


//#define LOCAL
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define mem(a, b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define DNF 0x7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define p_queue priority_queue
#define eps 1e-8
ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}
int n , m , k;
int st, ed, level[200], cur[200];
struct node{
    int t, next, c;
}edge[4500];
int head[200], cnt = 0;
void add(int f, int t, int c)
{
    edge[cnt].t = t;
    edge[cnt].c = c;
    edge[cnt].next = head[f];
    head[f] = cnt ++;
}
struct e{
    int x, y;
}in[2500];
bool DinicBfs(int s, int e)
{
    mem(level,0);
    queue<int>q;
    q.push(s);
    level[s] = 1;
    while(!q.empty())
    {
        int root = q.front();
        q.pop();
        for(int i = head[root] ; i != -1 ; i = edge[i].next)
        {
            int v = edge[i].t, c = edge[i].c;
            if(!level[v] && c > 0)
            {
                level[v] = level[root] + 1;
                q.push(v);
            }
        }
    }
    return level[e] != 0;
}
int DinicDfs(int root , int flow)
{
    if(root == ed)
        return flow;
    int newflow , cost = 0;
    for(int& i = cur[root] ; i != -1 ; i = edge[i].next)
    {
        int v = edge[i].t, c = edge[i].c;
        if(level[v] == level[root] + 1 && c > 0 && (newflow = DinicDfs(v,min(c,flow-cost))))
        {
            cost += newflow;
            edge[i].c -= newflow;
            edge[i^1].c += newflow;
            if(cost == flow) break;
        }
    }
    return cost;
}
int Dinic()
{
    int ans = 0;
    while(DinicBfs(st,ed))
    {
        for(int i = 0 ; i <= n*3 ; i ++)
            cur[i] = head[i];
        while(int add = DinicDfs(st,INF))
            ans += add;
    }
    return ans;
}
int main(void)
{
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("odata.out", "w", stdout);
#endif
    while(scanf("%d %d",&n, &m) == 2)
    {
        int ans = INF;
        char str[100];
        for(int i = 1; i <= m ; i ++)
        {
            int j;
            scanf("%s",str);
            for(in[i].x = 0, j = 1 ; str[j] != ',' ; j ++)
                in[i].x = in[i].x * 10 + str[j]-'0';
            for(in[i].y = 0, j ++ ; str[j] != ')' ; j ++)
                in[i].y = in[i].y * 10 + str[j]-'0';
            in[i].x ++ , in[i].y ++;
        }
        for(st = 1;st <= n;++ st)
            for(ed = 1;ed <= n;++ ed)
                if(st != ed)
                {
                    //除了拆点边容量为1,其他均为INF
                    memset(head,-1,sizeof(head));
                    cnt = 0;
                    for(int i = 1 ; i <= n; ++ i)
                        if(i == st || i == ed) add(i ,i+n,INF), add(i+n,i,0);
                        else add(i,i+n,1), add(i+n,i,0);
                    for(int i = 1 ; i <= m ; ++ i)
                    {
                        add(in[i].x+n,in[i].y,INF);
                        add(in[i].y,in[i].x+n,0);
                        add(in[i].y+n,in[i].x,INF);
                        add(in[i].x,in[i].y+n,0);
                    }
                    ans = min(ans,Dinic());
                }
        if(n <= 1 || ans == INF) ans = n;
        printf("%d\n",ans);
    }
    return 0;
}



版权声明:本文为Zenith_Habitant原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。