POJ1815_Friendship
一个无向图,问你删除多少点后,可以隔断起点到终点的所有路径?输出字典序最小的删点方案。
求最小点割,先拆点,容量为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的更多相关文章
随机推荐
- eclipse下载与安装并测试
下载地址:www.ecplise.org 下载完成后双击安装 安装完成之后,第一次运行eclipse会弹出Workspace Launcher对话框,要求设置工作空间存放项目文档. ...
- Linux 和 Windows 之间共享文件之 samba
导语 如果对windows有过实际操作技巧的人都会明白,在windows下的文件共享加上网络驱动器映射是多么方便的体验,甚至比ftp更加的简单,就像本地多了一块可与他人交流的硬盘一样. 问题 由于性能 ...
- STM8S——Universal asynchronous receiver transmitter (UART)
UART基本介绍: 通用异步收发器UART他的功能非常强大 我们只使用UART的全双工异步通信功能,使用中断接收数据. UART_RX:串行数据输入. UART_TX:串行数据输出. 硬件支持: 连接 ...
- 《python 经典实例》 分享 pdf下载
链接:https://pan.baidu.com/s/1FzSsBfynqx5ll_OpcZGDHg提取码:ykgk
- navicat连接mysql出现1251错误
刚刚安装热乎的navicat发现出现1251错误,原因不大清楚,找到一个解决办法: 将mysql的密码重新重置一遍: 1.打开命令行 ,进入mysql所在的目录,输入 mysql -uroot -p ...
- python多线程创建与使用(转)
原文:http://codingpy.com/article/python-201-a-tutorial-on-threads/ 创建多线程 创建多线程主要有2种方式. 使用threading.Thr ...
- 记录一个IIS的服务器错误问题的解决方案
部署一个mvc项目到iis的时候提示有下面这样的错误, 看提示是Microsoft.CodeDom.Providers.DotNetCompilerPlatform,权限问题. 我是第一次遇到,所以只 ...
- ROS (Robot Operating System) 相关资料与文档
本博文持续更新…… ROS学习与实践保护很多资料,最权威的当然是官网: www.ros.org 当然也有很多其他博文,尤其是中文文档有助于理解.2017推出一篇新博文,概况的资料较全. http:// ...
- 7.openldap使用ssl加密认证
作者:yaoyao 1.服务器端部署 1.自建CA中心 1.CA中心生成自身私钥 #cd /etc/pki/CA #(umask 077; openssl genrsa -out private/ca ...
- dos2unix命令详解
基础命令学习目录首页 原文链接:https://blog.csdn.net/leedaning/article/details/53024290 使用git 的时候碰到git将unix换行符转换为wi ...