POJ 1815 Friendship (Dinic 最小割)

Friendship
Time Limit: 2000MS   Memory Limit: 20000K
Total Submissions: 8025   Accepted: 2224

Description

In modern society, each person has his own friends. Since all the people are very busy, they communicate with each other only by phone. You can assume that people A can keep in touch with people B, only if 
1. A knows B's phone number, or 
2. A knows people C's phone number and C can keep in touch with B. 
It's assured that if people A knows people B's number, B will also know A's number.

Sometimes, someone may meet something bad which makes him lose touch with all the others. For example, he may lose his phone number book and change his phone number at the same time.

In this problem, you will know the relations between every two among N people. To make it easy, we number these N people by 1,2,...,N. Given two special people with the number S and T, when some people meet bad things, S may lose touch with T. Your job is to compute the minimal number of people that can make this situation happen. It is supposed that bad thing will never happen on S or T.

Input

The first line of the input contains three integers N (2<=N<=200), S and T ( 1 <= S, T <= N , and S is not equal to T).Each of the following N lines contains N integers. If i knows j's number, then the j-th number in the (i+1)-th line will be 1, otherwise the number will be 0.

You can assume that the number of 1s will not exceed 5000 in the input.

Output

If there is no way to make A lose touch with B, print "NO ANSWER!" in a single line. Otherwise, the first line contains a single number t, which is the minimal number you have got, and if t is not zero, the second line is needed, which contains t integers in ascending order that indicate the number of people who meet bad things. The integers are separated by a single space.

If there is more than one solution, we give every solution a score, and output the solution with the minimal score. We can compute the score of a solution in the following way: assume a solution is A1, A2, ..., At (1 <= A1 < A2 <...< At <=N ), the score will be (A1-1)*N^t+(A2-1)*N^(t-1)+...+(At-1)*N. The input will assure that there won't be two solutions with the minimal score.

Sample Input

3 1 3
1 1 0
1 1 1
0 1 1

Sample Output

1
2

Source

题意:有n个人,他们两两之间能够联系的要求是直接能够联系或者能同时联系到某一个人,现在给出A B两人,问最少删除多少个人后,A B无法联系,如果有多种方案,输出字典序最小的

算法:枚举+最大流

每个点拆点,之间连一条容量为1的弧,保证每个人最多被删掉一次

新增源点s汇点t,s只与x相连,t只与y’相连,容量均为+oo

如果i能与j联系,则在I’与J之间连一条容量为1的弧

求最大流并记录

如果x与y直接相连,则输出no answer并退出,如果x与y不连通(即最大流量为0),则输出0

然后从1到n一次删除该点(如果是x或y则不操作),如果删除该点后,最大流不变,则该点不是必要的,再恢复,最后得到tot值,即为删去的点,在枚举时用b数组标记每个点是否被删去,最后将被删去的点依次输出,满足字典序的条件

首先是构图,比较容易看出,这是最小割可以做的题目。

因为是无向图

首先是将每个点拆点,比如将i点拆成i,i+n点,两点之间连边,边长为1,这么设置可以让每个人的作用只出现一次,也能被最小割割到。

接着就是若两点之间相连,那么就让i+n和j相连,容量为无穷大。从而构造了最小割模型

注意S,T这两个点的构造有所不同,连向S+N,T+N的边的容量是无穷大,这样可以让他们不被最小割割中。。。

这个模型可以说是比较经典的一个模型了

主要是按字母序输出这个比较麻烦。

因为人数是一定的,可以依次枚举。

每次枚举一个人,把和他的联系全部断开,重新构图。

如果断开这个人的联系得到的流比原流小,那么就将原流减一,并对这个人做记录。如果不比原流小,那么就还原图

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int VM=;
const int EM=;
const int INF=0x3f3f3f3f; struct Edge{
int to,nxt;
int cap;
}edge[EM<<]; int n,cnt,head[VM],src,des;
int dep[VM]; void addedge(int cu,int cv,int cw){ //虽然是无向图建边,但是是拆点建边,所以反向边依然为0
edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].nxt=head[cu];
head[cu]=cnt++;
edge[cnt].to=cu; edge[cnt].cap=; edge[cnt].nxt=head[cv];
head[cv]=cnt++;
} int BFS(){
queue<int> q;
while(!q.empty())
q.pop();
memset(dep,-,sizeof(dep));
dep[src]=;
q.push(src);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(edge[i].cap> && dep[v]==-){
dep[v]=dep[u]+;
q.push(v);
}
}
}
return dep[des]!=-;
} int DFS(int u,int minx){
if(u==des)
return minx;
int tmp;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(edge[i].cap> && dep[v]==dep[u]+ && (tmp=DFS(v,min(minx,edge[i].cap)))){
edge[i].cap-=tmp;
edge[i^].cap+=tmp;
return tmp;
}
}
dep[u]=-;
return ;
} int Dinic(){
int ans=,tmp;
while(BFS()){
while(){
tmp=DFS(src,INF);
if(tmp==)
break;
ans+=tmp;
}
}
return ans;
} int s,t,map[VM][VM],g[VM][VM]; void buildgraph(){
cnt=;
memset(head,-,sizeof(head));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(i!=j){
if(map[i][j]==) //拆点建边,连通着的两点边权为INF保证不会成为割边
addedge(n+i,j,INF);
}else{
if(i==s || i==t) //s点,t点,题意表明不会出现问题,即拆成的两点不会成为割边
addedge(i,n+i,INF);
else
addedge(i,n+i,); //其余点则有可能成为割边,边权为1保证只切割一次
}
} int main(){ //freopen("input.txt","r",stdin); int res[VM];
while(~scanf("%d%d%d",&n,&s,&t)){
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&map[i][j]);
if(map[s][t]==){
puts("NO ANSWER!");
continue;
}
buildgraph();
src=s, des=n+t;
int ans=Dinic(); //原始最小割,即原来最多的相互联系的人数
printf("%d\n",ans);
if(ans==)
continue;
int tot=;
for(int i=;i<=n;i++){ //枚举去掉每一个人,(不包括s和t)
if(i==s || i==t)
continue;
//memcpy(g,map,sizeof(map)); //用这个函数超时啊,下同
for(int j=;j<=n;j++)
for(int k=;k<=n;k++){
g[j][k]=map[j][k];
if(j==i || k==i)
map[j][k]=;
}
buildgraph();
int tmp=Dinic();
if(ans>tmp){
ans--; //ans>tmp说明去掉这个人,联系变少了,所以原始最大流减一,并保存该人的id号
res[tot++]=i;
}else{
//memcpy(map,g,sizeof(g));
for(int j=;j<=n;j++) //最大流不变则恢复原图
for(int k=;k<=n;k++)
map[j][k]=g[j][k];
}
if(tmp==) //当tmp==0时,说明s与t已经无法联系了,所以得到的是最优值,-->结束
break;
}
for(int i=;i<tot-;i++)
printf("%d ",res[i]);
printf("%d\n",res[tot-]);
}
return ;
}
上一篇:最新WingIDE注册破解方法 【转】


下一篇:智能车学习(三)—— ADC学习