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

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
思路:
感谢llw大佬的神奇优化!!!!
耗时两天,终于AC了,总算是证明了自己的算法没有错误。
网上遍地都是拆点的题解,奈何我实在是太弱了,弱到无法想象的地步,实在是看不懂拆点应该要怎么拆,所以我被逼无奈选择了暴力。
那就是,限制一次dfs只能跑出容量为1的流,并将这条流上的所有点标记为不可用,直到一次Dinic结束。
以此取代拆点的操作,是完全可行的,其他的操作都大体相同
终于终于,体会到了AC的快乐,看着题解写出来的题,渐渐地成为了我自闭的根源
代码
邻接矩阵
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
int mp[300][300];
int n,s,t;
const int inf = 2100000000;
int vis[400],num[400];
bool book[400]; struct edge
{
int u,v,next,w;
}e[400010];
int Head[400010],cur;
int ans[400010]; void add(int x,int y)
{ e[cur].u=x;
e[cur].v=y;
e[cur].next=Head[x];
e[cur].w=1;
Head[x]=cur;
cur++;
e[cur].u=y;
e[cur].v=x;
e[cur].next=Head[y];
e[cur].w=0;
Head[y]=cur;
cur++;
} bool bfs()
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++){
num[i]=Head[i];
}
vis[s]=1;
queue<int>q;
q.push(s);
int r=0;
while(!q.empty()){
int u=q.front();
q.pop();
int k=Head[u];
while(k!=-1){
if(!book[e[k].v]&&!vis[e[k].v]&&e[k].w){
vis[e[k].v]=vis[u]+1;
q.push(e[k].v);
}
k=e[k].next;
}
}
return vis[t];
} int dfs(int u)
{
if(u==t){return 1;} int &k=num[u];
while(k!=-1){
if(!book[e[k].v]&&vis[e[k].v]==vis[u]+1&&e[k].w){
int d=dfs(e[k].v);
e[k].w-=d;
e[k^1].w+=d;
if(d>0){book[u]=true;return 1;}
}
k=e[k].next;
}
return 0;
} void build()
{
cur=0;
memset(Head,-1,sizeof(Head));
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(mp[i][j]) add(i,j);
}
}
} int Dinic(int y)
{
memset(book,0,sizeof(book));
book[y]=1;
int ans=0;
while(bfs()){
int f;
while((f=dfs(s))>0){//llw大佬太强了orz orz orz
ans+=f;
}
}
return ans;
} inline void rebuild(int tt)
{
for(int i=0;i<=cur;i+=2){
if(e[i].v==tt||e[i].u==tt){e[i].w=e[i^1].w=0;}
else if(e[i^1].w!=0){
e[i].w=1;
e[i^1].w=0;
}
} }
int main()
{
scanf("%d%d%d",&n,&s,&t);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&mp[i][j]);
}
}
if(mp[s][t]==1){printf("NO ANSWER!\n");return 0;}
build();
int maxx=Dinic(0);
rebuild(0);
int num=0;
for(int i=1;i<=n;i++){
if(i==s||i==t){continue;}
int y=Dinic(i);
if(y<maxx){
ans[++num]=i;
maxx=y;
if(maxx==0){break;}
rebuild(i);
}
else rebuild(0);
}
if(num==0){printf("0\n");return 0;}
printf("%d\n",num);
for(int i=1;i<=num;i++){
if(i!=1){printf(" ");}
printf("%d",ans[i]);
}
printf("\n");
}

  

 

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

  1. POJ 1815 Friendship (Dinic 最小割)

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

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

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

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

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

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

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

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

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

  6. POJ 3281 Dining (网络流)

    POJ 3281 Dining (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...

  7. POJ 2431 Expedition(探险)

    POJ 2431 Expedition(探险) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A group of co ...

  8. POJ 3414 Pots(罐子)

    POJ 3414 Pots(罐子) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 You are given two po ...

  9. POJ 1847 Tram (最短路径)

    POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...

随机推荐

  1. Promise是什么?

    一. Promise是什么? Promise是一种异步操作的解决方案,将写法复杂的传统的回调函数和监听事件的异步操作,用同步代码的形式表达出来. 避免了多级异步操作的回调函数嵌套. Promise最早 ...

  2. $mount(“#app”)手动挂载

    没有el属性时,证明vue还没绑定到特定的dom上,需要延迟加载,则使用.$mount("")进行手动挂载 https://blog.csdn.net/longzhoufeng/a ...

  3. JavaScript之Json的使用

    Json字符串转JavaScript对象 <html> <body> <h3>通过 JSON 字符串来创建对象</h3> <p> First ...

  4. LODOOP中的各种边距 打印项、整体偏移、可打区域、内部边距

    Lodop中的打印项内容位置定位,除了打印项本身的top,left值,也会受其他设定或打印机的影响.打印开发,先用虚拟打印机测试出正确结果,然后客户端用打印维护微调常见问题:1.设置打印项相对于纸张居 ...

  5. 二、Java多人博客系统-演变

    任何项目都是由小到大,逐步演变的.自己写的这个博客系统也不例外. 更新日志如下: 一.2014年底-2015年初 功能及技术: 1.定位为个人网站,介绍自己情况和发布文章等. 2.首页模块有:个人简介 ...

  6. Civil 3D 2017本地化中VBA程序移植到2018版中

    中国本地化包简直就是一块鸡肋, 但对于某些朋友来说还真离不了: 可惜中国本地化包的推出一直滞后, 在最新版软件出来后1年多, 本地化还不一定能够出来, 即使出来了, 也只能是购买了速博服务的用户才能得 ...

  7. ng-click 发两次ajax请求的原因及解决方法

    http://blog.csdn.net/anmo/article/details/17083125

  8. Typecho——简介及安装

    Typecho Typecho是由type和echo两个词合成的,来自于开发团队的头脑风暴.Typecho基于PHP5开发,支持多种数据库,是一款内核强健﹑扩展方便﹑体验友好﹑运行流畅的轻量级开源博客 ...

  9. JavaWeb项目自动部署,持续集成

    来公司以后,学会两种JavaWeb项目,自动部署. 1.jenkins持续集成.自动化部署 (1)安装jenkins----------推荐nginx跳转方式,以域名方式 (2)nginx采用不同域名 ...

  10. codeforces 1065F Up and Down the Tree

    题目链接:codeforces 1065F Up and Down the Tree 题意:给出一棵树的节点数\(n\)以及一次移动的最大距离\(k\),现在有一个标记在根节点1处,每一次可以进行一下 ...