POJ 1815 Friendship(最小割+字典序输出割点)
http://poj.org/problem?id=1815
题意:
在现代社会,每个人都有自己的朋友。由于每个人都很忙,他们只通过电话联系。你可以假定A可以和B保持联系,当且仅当:①A知道B的电话号码;②A知道C的电话号码,而C能联系上B。如果A知道B的电话号码,则B也知道A的电话号码。
思路:
这题是要我们删点,既然是删点,那么就要拆点,容量就是1。
接下来凡是能联系的,就连边,容量为INF,因为我们不是要删除这些边。跑遍最大流就能算出至少要删除多少个点。
这道题的关键是要字典序顺序输出最小割的点集,在跑一遍最大流之后,我们按字典序顺序依次枚举每个点,将该点删除后然后再去跑最大流,如果流量减少了,说明它是割点。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = + ; struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int w,int f):from(u),to(v),cap(w),flow(f){}
}; struct Dinic
{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int cur[maxn];
int d[maxn]; void init(int n)
{
this->n=n;
for(int i=;i<n;++i) G[i].clear();
edges.clear();
} void AddEdge(int from,int to,int cap)
{
edges.push_back( Edge(from,to,cap,) );
edges.push_back( Edge(to,from,,) );
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BFS()
{
queue<int> Q;
memset(vis,,sizeof(vis));
vis[s]=true;
d[s]=;
Q.push(s);
while(!Q.empty())
{
int x=Q.front(); Q.pop();
for(int i=;i<G[x].size();++i)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=true;
d[e.to]=d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x,int a)
{
if(x==t || a==) return a;
int flow=, f;
for(int &i=cur[x];i<G[x].size();++i)
{
Edge &e=edges[G[x][i]];
if(d[e.to]==d[x]+ && (f=DFS(e.to,min(a,e.cap-e.flow) ) )>)
{
e.flow +=f;
edges[G[x][i]^].flow -=f;
flow +=f;
a -=f;
if(a==) break;
}
}
return flow;
} int Maxflow(int s,int t)
{
this->s=s; this->t=t;
int flow=;
while(BFS())
{
memset(cur,,sizeof(cur));
flow +=DFS(s,INF);
}
return flow;
}
}DC; int n,s,t;
int mp[maxn][maxn]; int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d%d",&n,&s,&t))
{
int dst=*n+;
DC.init(dst+); for(int i=;i<=n;i++)
{
if(i==s || i==t) DC.AddEdge(i,i+n,INF);
else DC.AddEdge(i,i+n,);
} for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&mp[i][j]);
if(i==j) continue;
if(mp[i][j]) DC.AddEdge(i+n,j,INF);
}
} if(mp[s][t]) {puts("NO ANSWER!");continue;} int cnt=DC.Maxflow(s,t);
vector<int> ans; printf("%d\n",cnt);
if(cnt==) continue; for(int i=;i<*n;i+=) //前2*n条边为每个顶点,依次枚举
{
Edge& e=DC.edges[i];
if(e.from==s || e.from==t) continue;
e.cap=; //删边
for(int j=;j<*n;j++) DC.edges[j].flow = ;
int tmp=DC.Maxflow(s,t); //如果流量改变,说明这条边是割边
if(tmp<cnt)
{
cnt=tmp;
ans.push_back(e.from);
}
else e.cap=;
if(cnt<=) break;
}
for(int i=;i<ans.size();i++)
{
printf("%d%c",ans[i],i==ans.size()-?'\n':' ');
}
printf("\n");
}
return ;
}
POJ 1815 Friendship(最小割+字典序输出割点)的更多相关文章
- POJ 1815 Friendship(最小割)
		
http://poj.org/problem? id=1815 Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissio ...
 - poj 1815 Friendship (最小割+拆点+枚举)
		
题意: 就在一个给定的无向图中至少应该去掉几个顶点才干使得s和t不联通. 算法: 假设s和t直接相连输出no answer. 把每一个点拆成两个点v和v'',这两个点之间连一条权值为1的边(残余容量) ...
 - POJ - 1815 Friendship (最小点割集)
		
(点击此处查看原题) 题目分析 题意:有n个人,编号记为1~n,n个人之间可能有人可以互相联系,如果A能和B联系,那么至少满足这两种情况之一:(1)A知道B的电话(2)A可以和C联系,并且C可以和B联 ...
 - poj 1815(最小割、割集)
		
题目链接:http://poj.org/problem?id=1815 思路:题目要求是剔除多少个点,可以将其转化为剔除多少条边,因此需要拆点,将点i拆成i,i+n,便容量为1,表示每个人起的传递作用 ...
 - POJ 1815 Friendship(字典序最小的最小割)
		
Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissions: 10744 Accepted: 2984 Descr ...
 - poj 1815 Friendship 字典序最小+最小割
		
题目链接:http://poj.org/problem?id=1815 In modern society, each person has his own friends. Since all th ...
 - POJ  1815  Friendship (Dinic 最小割)
		
Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissions: 8025 Accepted: 2224 Descri ...
 - POJ 1815 Friendship(最大流最小割の字典序割点集)
		
Description In modern society, each person has his own friends. Since all the people are very busy, ...
 - POJ 1815 Friendship ★(字典序最小点割集)
		
[题意]给出一个无向图,和图中的两个点s,t.求至少去掉几个点后才能使得s和t不连通,输出这样的点集并使其字典序最大. 不错的题,有助于更好的理解最小割和求解最小割的方法~ [思路] 问题模型很简单, ...
 
随机推荐
- WannaCry应急排查思路
			
一.绪论: WannaCry是一款基于NSA的永恒之蓝漏洞(SMB-MS17-010)类似蠕虫似传播的一款勒索软件(Ransomware).一旦中招,该勒索病毒会对系统中的各种文件进行加密,比要求支付 ...
 - 【BZOJ4429】[Nwerc2015] Elementary Math小学数学 最大流
			
[BZOJ4429][Nwerc2015] Elementary Math小学数学 Description Ellen给她的学生教小学数学.期末考试已经来临了.考试有n个题目,每一个题目学生们都要对一 ...
 - dubbo用途介绍
			
转自:http://blog.csdn.net/wuliu_forever/article/details/52053928 我们讨论过Nginx+tomcat组成的集群,这已经是非常灵活的集群技术, ...
 - explain   分析 聚合统计语句的性能
			
EXPLAIN SELECT COUNT(1) FROM question; id select_type table partitions type possible_keys key key_le ...
 - Benefits of Using the Spring Framework  Dependency Injection  依赖注入   控制反转
			
小结: 1. Dependency Injection is merely one concrete example of Inversion of Control. 依赖注入是仅仅是控制反转的一个具 ...
 - .m2\repository\org\springframework\spring-beans\4.1.4.RELEASE\spring-beans-4.1.4.RELEASE.jar!\org\springframework\beans\factory\xml\spring-beans-4.1.xsd
			
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <xsd:s ...
 - ruby rvm groke
			
https://ruby-china.org/wiki/rvm-guide http://www.cnblogs.com/peak-c/p/7338291.html https://doc.yonyo ...
 - RGBA HSB opengl光照模型
			
RGBA HSB HSV颜色模型对应于画家的配色的方法.画家用改变色浓和色深的方法来从某种纯色获得不同色调的颜色.其做法是:在一种纯色中加入白色以改变色浓,加入黑色以改变色深,同时加入不同比例的白 ...
 - php基础:面向对象
			
一.public.private.protected访问修饰符 public:任何都可以访问(本类.子类.外部都可以访问) protected:本类.子类都可以访问(本类.子类均可访问) privat ...
 - 用 mongodb 储存多态消息/提醒类数据(转)
			
原文:http://codecampo.com/topics/66 前天看到 javaeye 计划采用mongoDB实现网站全站消息系统,很有同感,mongodb 很适合储存消息类数据.之前讨论了如何 ...