一个无向图,问你删除多少点后,可以隔断起点到终点的所有路径?输出字典序最小的删点方案。

求最小点割,先拆点,容量为1,普通边容量无穷,最大流即为应删点数。

需要求出字典序最小的方案,可以从小到大枚举所有的点,如果当前枚举的点是割点,那么进行标记,同时后面的枚举也不再经过这个点。

召唤代码君:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#define maxn 5555
#define maxm 555555
using namespace std; int to[maxm],next[maxm],c[maxm],belong[maxm],first[maxm],edge;
int d[maxn],tag[maxn],TAG=;
bool can[maxn],go[maxn];
int Q[maxn],bot,top;
int f[][];
int s,t,n,pos[maxn]; void _init()
{
edge=-;
for (int i=; i<=n+n; i++) first[i]=-,go[i]=false;
} void addedge(int U,int V)
{
edge++;
to[edge]=V,c[edge]=,next[edge]=first[U],first[U]=edge;
edge++;
to[edge]=U,c[edge]=,next[edge]=first[V],first[V]=edge;
} bool bfs()
{
Q[bot=top=]=t,tag[t]=++TAG,d[t]=,can[t]=false;
while (bot<=top)
{
int cur=Q[bot++];
for (int i=first[cur]; i!=-; i=next[i])
if (c[i^]> && tag[to[i]]!=TAG && !go[to[i]])
{
tag[to[i]]=TAG,d[to[i]]=d[cur]+;
can[to[i]]=false,Q[++top]=to[i];
if (to[i]==s) return true;
}
}
return false;
} int dfs(int cur,int num)
{
if (cur==t) return num;
int tmp=num,k;
for (int i=first[cur]; i!=-; i=next[i])
if (c[i]> && tag[to[i]]==TAG && d[to[i]]==d[cur]- && !can[to[i]])
{
k=dfs(to[i],min(num,c[i]));
if (k) num-=k,c[i]-=k,c[i^]+=k;
if (!num) break;
}
if (num) can[cur]=true;
return tmp-num;
} int maxflow()
{
int flow=;
while (bfs()) flow+=dfs(s,~0U>>);
return flow;
} int get(int x)
{
for (int i=first[x]; i!=-; i=next[i])
if (c[i]== && !(i&))
{
int tmp=to[i];
tmp-=x;
if (tmp<) tmp=-tmp;
if (tmp!=n) return get(to[i]);
tmp=min(x,to[i]);
return min(tmp,get(to[i]));
}
return ~0U>>;
} int main()
{
int tmp,mf;
vector<int> ans;
while (scanf("%d%d%d",&n,&s,&t)!=EOF)
{
_init();
for (int i=; i<=n; i++) addedge(i,i+n),pos[i]=edge;
for (int i=; i<=n; i++)
for (int j=; j<=n; j++)
{
scanf("%d",&tmp);
f[i][j]=f[j][i]=tmp;
if (i>=j) continue;
if (tmp) addedge(i+n,j),addedge(j+n,i);
}
if (s==t || f[s][t])
{
puts("NO ANSWER!");
continue;
}
s+=n;
ans.clear();
mf=maxflow();
printf("%d\n",mf);
if (mf==) continue;
for (int i=; i<=n && mf>; i++)
{
if (i==s-n || i==t) continue;
for (int j=; j<edge; j+=) c[j]+=c[j+],c[j+]=;
go[i]=true,go[i+n]=true;
tmp=maxflow();
if (tmp<mf) ans.push_back(i),mf--;
else go[i]=false,go[i+n]=false;
} sort(ans.begin(),ans.end());
printf("%d",ans[]);
for (unsigned i=; i<ans.size(); i++) printf(" %d",ans[i]);
printf("\n");
}
return ;
}

POJ1815_Friendship的更多相关文章

随机推荐

  1. 三剑客之sed&grep

    第1章 练习题 1.1 第1题 取得/etc/hosts 文件的权限 如何取得/etc/hosts 文件的权限对应的数字内容,如-rw-r--r--  为 644,要求使用命令取得644 这样的数字. ...

  2. lua编程之元表与元方法

    一. 前言 lua是一种非常轻量的动态类型语言,在1993年由由Roberto Ierusalimschy.Waldemar Celes 和 Luiz Henrique de Figueiredo等人 ...

  3. Python科学计算库-Numpy

    NumPy 是 Python 语言的一个扩充程序库.支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库,也是学习 python 必学的一个库. 1. 读取文件 numpy.gen ...

  4. ssh连接超时自动断掉的解决办法

    最近开始使用阿里云服务器ECS,系统是CENTOS7.4 ,通过SSH连接上自动创建的实例后,几分钟不操作,就自动断开,提示“信号灯时间已到”,非常影响工作. 解决办法: 默认镜像创建的实例,SSH服 ...

  5. Unity3D之AR开发(一)

    近期研究了下AR技术,下面给大家分享一下. 第一种方法:高通AR(Vuforia) Vuforia插件下载地址(官网): https://developer.vuforia.com/downloads ...

  6. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  7. Java的POI的封装与应用

    Java对Excel表格的导出一直是对我有种可怕噩梦的东西,每次对要建立行与列,并一个一个放值,我是从心底拒绝的. 处于项目需求,需要导出表格,于是找到网上一版很好的开发, <不想用POI?几行 ...

  8. OTRS

    更新OTRS root@localhost密码: sudo -u otrs /opt/otrs/bin/otrs.Console.pl Admin::User::SetPassword root@lo ...

  9. nodejs ejs模板数据库操作

    1. 建立数据连接setting.js var settings = {}; settings.db = { host: 'localhost', user: 'root', password: '1 ...

  10. Django_rest_framework_组件(authentication、permission、throttle)

    认证组件 说明 from rest_framework.authentication import BaseAuthentication class TestAuthentication(BaseAu ...