Friendship

Time Limit: 2000MS Memory Limit: 20000K

Total Submissions: 9824 Accepted: 2720

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

POJ Monthly

题意:给不同的人之间的联系,问至少有多少人遇到糟糕的事情会使S,T之间断开联系;

这个题需要拆点,将点拆成(i,i+N),之间的容量为一,遇到糟糕的事情就是自己与自己失去联系,通过最大流可以判断最小割,然后枚举点找字典序最最小的.

#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF =0x3f3f3f3f;
const double eps =1e-8;
const int Max =3000;
struct node
{
int cap;
int flow;
} Map[500][500];
int Set[500];
int Du[555];
bool vis[555];
int S,T,N;
int s,t;
void AddEdge(int u,int v,int w)
{
Map[u][v].cap = w;
}
bool BFS()
{
memset(Du,0,sizeof(Du));
memset(vis,false,sizeof(vis));
queue<int>Q;
Du[s]=1;
vis[s]=true;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=0; i<=t; i++)
{
if(!vis[i]&&Map[u][i].cap>Map[u][i].flow)
{
vis[i]=true;
Du[i]=Du[u]+1;
Q.push(i);
}
}
}
return vis[t];
}
int DFS(int star,int num)
{
if(star==t)
{
return num;
}
int a=0;
int ant;
for(int i=0; i<=t; i++)
{
if(Du[i]==Du[star]+1&&Map[star][i].cap>Map[star][i].flow)
{
if(ant=DFS(i,min(num,Map[star][i].cap-Map[star][i].flow)))
{
Map[star][i].flow+=ant;
Map[i][star].flow-=ant;
num-=ant;
a+=ant;
if(num==0)
{
break;
}
}
}
}
return a;
}
void Dinic()
{
int ans=0;
while(BFS())
{
ans+=DFS(0,INF);
}
printf("%d\n",ans);
if(!ans)
{
return ;
}
int cut=0;
int tmp=ans;
for(int i=1; i<=N&&tmp; i++)
{
if(i==S||i==T)
{
continue;
}
if(Map[i][i+N].flow==0)
{
continue;
}
Map[i][i+N].cap=0;
for(int a=1; a<=t; a++)
{
for(int b=1; b<=t; b++)
{
Map[a][b].flow=0;
}
}
ans=0;
while(BFS())
{
ans+=DFS(0,INF);
}
if(ans!=tmp)
{
Set[cut++]=i;
tmp=ans;
}
else
{
Map[i][i+N].cap=1;
}
}
for(int i=0; i<cut; i++)
{
if(i)
{
printf(" ");
}
printf("%d",Set[i]);
}
printf("\n");
}
int main()
{
int data;
while(~scanf("%d %d %d",&N,&S,&T))
{
memset(Map,0,sizeof(Map));
s=0;
t=N*2+1;
AddEdge(s,S,INF);
AddEdge(T+N,t,INF);
for(int i=1; i<=N; i++)
{
AddEdge(i,i+N,1);
for(int j=1; j<=N; j++)
{
scanf("%d",&data);
if(data)
{
AddEdge(i+N,j,INF);
}
}
}
AddEdge(S,S+N,INF);
AddEdge(T,T+N,INF);
if(!Map[S+N][T].cap)
{ Dinic();
}
else
{
printf("NO ANSWER!\n");
}
}
return 0;
}

Friendship的更多相关文章

  1. POJ 1815 Friendship

    Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissions: 10626   Accepted: 2949 Descr ...

  2. HDU 5578 Friendship of Frog 水题

    Friendship of Frog Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.ph ...

  3. WordPress BuddyPress Extended Friendship Request插件跨站脚本漏洞

    漏洞名称: WordPress BuddyPress Extended Friendship Request插件跨站脚本漏洞 CNNVD编号: CNNVD-201307-609 发布时间: 2013- ...

  4. Friendship of Frog(水题)

    Friendship of Frog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  5. Codeforces 791B Bear and Friendship Condition(DFS,有向图)

    B. Bear and Friendship Condition time limit per test:1 second memory limit per test:256 megabytes in ...

  6. codeforces round #405 B. Bear and Friendship Condition

    B. Bear and Friendship Condition time limit per test 1 second memory limit per test 256 megabytes in ...

  7. Bear and Friendship Condition-HZUN寒假集训

    Bear and Friendship Condition time limit per test 1 secondmemory limit per test 256 megabytesinput s ...

  8. POJ 1815 Friendship (Dinic)

    Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissions: 11429   Accepted: 3173 Descr ...

  9. 8. American Friendship 美国式的友谊

    8. American Friendship 美国式的友谊 (1) Americans usually consider themselves a friendly people.Their frie ...

随机推荐

  1. mongodb概念

    一.mongodb与关系型数据库的一些概念上的改变 sql术语 mongodb术语 说明 database database 数据库 table collection 表/集合 row documen ...

  2. Effective C++ 5.实现

    //条款26:尽量延后变量的定义式出现的时间 // 1.不仅应该延后变量的定义,更应该直到使用该变量的前一刻为止,甚至应该尝试延后这份定义直到能够给它初始值为止.如果这样,不仅能够避免构造和析构的非必 ...

  3. zabbix监控企业esxi虚拟机

    zabbix监控企业esxi虚拟机 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我来公司有一段时间了,最近也发现模型部有测试和开发反应某台机器登陆不上去了,结果登陆esxi服务器 ...

  4. configuring tortoise git and vs code.

    Preparation, SSH keygen: $ git config --global user.name "calos" $ git config --global use ...

  5. ACM常用算法及练习(1)

    ACM常用算法及练习 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码,因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打出来. 1.最短 ...

  6. paper 13:计算机视觉研究群体及专家主页汇总

    做机器视觉和图像处理方面的研究工作,最重要的两个问题:其一是要把握住国际上最前沿的内容:其二是所作工作要具备很高的实用背景.解决第一个问题 的办法就是找出这个方向公认最高成就的几个超级专家(看看他们都 ...

  7. js中把JSON字符串转换成JSON对象最好的方法

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 第一种解析方式:使用eval函数来解析,并且使用j ...

  8. java连接数据库URL

    转: 1.Oracle数据库 Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String url ...

  9. nginx 反向代理 和lvs负载均衡

    nginx反向代理:用户请求nginx代理服务器然后代理服务器将用户请求转为服务器再由nginx代理服务器将服务器的响应反应给用户. lvs负载均衡:用户请求nginx代理服务器然后代理服务器将用户请 ...

  10. Jni层回调java代码【转】

    本文转载自:http://www.linuxidc.com/Linux/2014-03/97562.htm JNI是Java Native Interface的缩写,是Java平台的重要特性,使得Ja ...