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. TSQL:判断某较短字符串在较长字符串中出现的次数。

    给定一个较短字符串shortStr='ab',和一个较长字符串longStr='adkdabkwelabwkereabrsdweo2342ablk234lksdfsdf1abe': 判断shortSt ...

  2. Altium Designer 基本封装

    1. 按键的绘制和封装怎么画?如下图: 回答:注意元件的画法不要看错了,封装采用Miscellaneous Devices.IntLib[Footprint View]中的DPST-4 2.蜂鸣器的绘 ...

  3. libsvm使用说明

    http://www.hankcs.com/ml/libsvm-usage.html libsvm使用说明 码农场 > 机器学习 2016-02-18 阅读(345) 评论(0)  目录   l ...

  4. Flask刷新问题

    修改页面中内容,特别是图片后,总是刷新不了.调试时,我常常通过修改端口来解决,从80-99不断改. 服务器部署,也遇到同样问题,重启web服务器,重启计算机都不行,网页已经改过来了,但是图片还是老图片 ...

  5. Pandas DataFrame笔记

    1.属性方式,可以用于列,不能用于行 2.可以用整数切片选择行,但不能用单个整数索引(当索引不是整数时) 3.直接索引可以使用列.列集合,但不能用索引名索引行 用iloc取行,得到的series: d ...

  6. installers PHPManager

    === Verbose logging started: // :: Build type: SHIP UNICODE 5.00.10011.00 Calling process: C:\Progra ...

  7. mysql下载安装使用教程

    今天花了点时间终于把mysql安装好了,哈哈!下面上干货! (1)从官网下载mysql.地址https://www.mysql.com/ 图:按照图上的步骤就能找到 a.首页点击    DOWNLOA ...

  8. Masonry应用【美图秀秀首页界面自动布局】

    Masonry在此实现时候,并没有比NSLayoutConstraint简单,相反我觉得还不如NSLayoutConstraint. [self.topView mas_makeConstraints ...

  9. AWR - Load Profile 节

    AWR 报告的"Load profile"节,如下图所示,包含很多极为有用,却被经常忽视的信息.通常更倾向使用"instance efficiency percentag ...

  10. js未分类

    jQuery设置透明 1.直接.fadeIn 淡入 .fadeOut 淡出 .fadeTo(时间,"透明度") 2.addClass只能控制淡入和淡出,不能控制其速度. filte ...