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的更多相关文章
随机推荐
- CSS快速入门-实用技巧
1.整体布局 大部分的布局都是由三部分组成,header.body.footer. 代码布局:写三个div <!DOCTYPE html> <html lang="en&q ...
- 在使用Reference Source调试.Net 源代码时如何取消optimizations(代码优化)-翻译
在使用PDB调试XAF时,发现好多变量都看不到.都被优化掉了. 下面的方法可以解决. 当你在使用Reference Source functionality in VS 2008 调试.Net 的源代 ...
- 运行用例时,报错Unknow Error:Element xxx is not clickable……的解决方法
P.S:近期selenium官方更新了版本以解决此问题 通常这种情况是由于在点击该元素时,js更换了元素属性造成的. 所以可以采用js的方式进行处理 方法如下: WebDriver driver = ...
- linux用命令行编译使用函数库
同步于气象家园日志 from fcode 视频 编译静态链接库 gfortran -c sub.f90 func.f90 产生了func.mod文件.注:mod文件是静态库的接口.如果删掉了fu ...
- selenium 定位不到元素总结
元素在网页上,却会出现定位不到的情况的分析. 1. 定位不正确. 2. 页面还没有加载完就去查找元素了. 3. 有遮罩层. 首先说下第3点. 先前在公司遇到过这样的问题. 页面是显示出来了, 这个元素 ...
- python simple factory mode example
Two python simple factory mode examples shown in this section. One is for base operation and another ...
- 如何配置pycaffe
首先,使用cmake配置.生成caffe的vs2015工程时,设定生成python接口,即BUILD项->BUILD_python.BUILD_python_layer,注意使用CMake生成V ...
- RabbitMQ入门:远程过程调用(RPC)
假如我们想要调用远程的一个方法或函数并等待执行结果,也就是我们通常说的远程过程调用(Remote Procedure Call).怎么办? 今天我们就用RabbitMQ来实现一个简单的RPC系统:客户 ...
- Dubbo问题处理集合
1 . 启动微服务的时候,报错信息如下: 核心:Can not lock the registry cache file /root/.dubbo/dubbo-registry-127.0.0.1.c ...
- 高可用OpenStack(Queen版)集群-4.keystone集群
参考文档: Install-guide:https://docs.openstack.org/install-guide/ OpenStack High Availability Guide:http ...