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 ;
}

POJ 1815 Friendship (Dinic 最小割)的更多相关文章

  1. POJ 1815 Friendship(最小割+字典序输出割点)

    http://poj.org/problem?id=1815 题意: 在现代社会,每个人都有自己的朋友.由于每个人都很忙,他们只通过电话联系.你可以假定A可以和B保持联系,当且仅当:①A知道B的电话号 ...

  2. poj 1815 Friendship【最小割】

    网络流的题总是出各种奇怪的错啊--没写过邻接表版的dinic,然后bfs扫到t点不直接return 1就会TTTTTLE-- 题目中的操作是"去掉人",很容易想到拆点,套路一般是( ...

  3. poj 1815 Friendship 字典序最小+最小割

    题目链接:http://poj.org/problem?id=1815 In modern society, each person has his own friends. Since all th ...

  4. POJ 1815 Friendship ★(字典序最小点割集)

    [题意]给出一个无向图,和图中的两个点s,t.求至少去掉几个点后才能使得s和t不连通,输出这样的点集并使其字典序最大. 不错的题,有助于更好的理解最小割和求解最小割的方法~ [思路] 问题模型很简单, ...

  5. POJ 1815 Friendship(最大流最小割の字典序割点集)

    Description In modern society, each person has his own friends. Since all the people are very busy, ...

  6. POJ 1815 Friendship(字典序最小的最小割)

    Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissions: 10744   Accepted: 2984 Descr ...

  7. POJ 1815 Friendship(最小割)

    http://poj.org/problem? id=1815 Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissio ...

  8. poj 1815 Friendship (最小割+拆点+枚举)

    题意: 就在一个给定的无向图中至少应该去掉几个顶点才干使得s和t不联通. 算法: 假设s和t直接相连输出no answer. 把每一个点拆成两个点v和v'',这两个点之间连一条权值为1的边(残余容量) ...

  9. POJ - 1815 Friendship (最小点割集)

    (点击此处查看原题) 题目分析 题意:有n个人,编号记为1~n,n个人之间可能有人可以互相联系,如果A能和B联系,那么至少满足这两种情况之一:(1)A知道B的电话(2)A可以和C联系,并且C可以和B联 ...

随机推荐

  1. 什么是哈希码(HashCode)

    什么是哈希码(HashCode) 在Java中,哈希码代表对象的特征. 例如对象 String str1 = “aa”, str1.hashCode= 3104 String str2 = “bb”, ...

  2. 使用Robot Framework做接口测试

    http://chuansong.me/n/1858477 1.RF框架 1.1 RF框架介绍Robot Framework 框架是一个通用的测试框架,一直是由诺西网络(Nokia Siemens N ...

  3. ckeditor 4.2.1_演示 ckeditor 上传&插入图片

    本文内容 FineUI ckeditor fckeditor/ckeditor 演示 ckeditor 4.2.1 上传&插入图片 最近看了一下 FineUI_v3.3.1 控件,对里边的 c ...

  4. PHPCMS源码分析

    PHPCMS 一.模版引擎 如:调用单页面index.php?m=content&c=index&a=lists&catid=9.1.先获取到模版变量的值$template_l ...

  5. Windows 之 可以Ping通服务器但无法使用服务器连接的共享打印机

    故障现象:一个公司内部局域网中,一台电脑可以Ping通服务器,但无法使用服务器连接的共享打印机. 故障分析与排除:根据故障现象分析,由于客户端可以Ping通服务器,说明网络连接正常,故障可能是由客户端 ...

  6. Spring MVC中forward请求转发2种方式(带参数)

    Spring MVC中forward请求转发2种方式(带参数) http://www.51gjie.com/javaweb/956.html  

  7. ajax 与springmvc交互返回数据

    1.controller将数据封装成json格式返回页面 @RequestMapping("/dataList") public void datalist(CsoftCunsto ...

  8. WIP 001 - design the applicant screen

    In this item, you only need to design the screen

  9. SLF4J warning or error messages and their meanings

    来自:http://www.slf4j.org/codes.html#StaticLoggerBinder The method o.a.commons.logging.impl.SLF4FLogFa ...

  10. Android学习笔记三:用Intent串联activity

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7513399.html 一:Intent Intent可以理解为 意图. 我们可以通过创建intent实例来定义 ...