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

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

[Submit]   [Go Back]   [Status]   [Discuss]

求字典序最小的点集,割开S和T点。类比于无向图的点连通度。

笨方法就是跑N次最大流,貌似网上有一次最大流的算法,自己没YY出来,无限期……

 #include <cstdio>
#include <cstring> #define fread_siz 1024 inline int get_c(void)
{
static char buf[fread_siz];
static char *head = buf + fread_siz;
static char *tail = buf + fread_siz; if (head == tail)
fread(head = buf, , fread_siz, stdin); return *head++;
} inline int get_i(void)
{
register int ret = ;
register int neg = false;
register int bit = get_c(); for (; bit < ; bit = get_c())
if (bit == '-')neg ^= true; for (; bit > ; bit = get_c())
ret = ret * + bit - ; return neg ? -ret : ret;
} template <class T>
inline T min(T a, T b)
{
return a < b ? a : b;
} const int N = ;
const int inf = 2e9;
const int maxn = ; int n;
int ans;
int S, T;
int G[N][N]; int s, t;
int edges;
int hd[];
int to[maxn];
int fl[maxn];
int nt[maxn]; inline void add(int u, int v, int f)
{
nt[edges] = hd[u]; to[edges] = v; fl[edges] = f; hd[u] = edges++;
nt[edges] = hd[v]; to[edges] = u; fl[edges] = ; hd[v] = edges++;
} int dep[]; inline bool bfs(void)
{
static int que[maxn];
static int head, tail; memset(dep, , sizeof(dep));
head = , tail = ;
que[tail++] = s;
dep[s] = ; while (head != tail)
{
int u = que[head++], v;
for (int i = hd[u]; ~i; i = nt[i])
if (!dep[v = to[i]] && fl[i])
{
dep[v] = dep[u] + ;
que[tail++] = v;
}
} return dep[t];
} int dfs(int u, int f)
{
if (u == t || !f)
return f; int used = , flow, v; for (int i = hd[u]; ~i; i = nt[i])
if (dep[v = to[i]] == dep[u] + && fl[i])
{
flow = dfs(v, min(fl[i], f - used)); used += flow;
fl[i] -= flow;
fl[i^] += flow; if (used == f)
return f;
} if (!used)
dep[u] = ; return used;
} inline int maxFlow(void)
{
int maxFlow = , newFlow; while (bfs())
while (newFlow = dfs(s, inf))
maxFlow += newFlow; return maxFlow;
} int vis[maxn]; signed main(void)
{
n = get_i();
S = get_i() - ;
T = get_i() - ; for (int i = ; i < n; ++i)
for (int j = ; j < n; ++j)
G[i][j] = get_i(); if (G[S][T])
return puts("NO ANSWER!"), ; memset(hd, -, sizeof(hd)); for (int i = ; i < n; ++i)
for (int j = ; j < n; ++j)if (G[i][j])
add(i << , j << | , inf); for (int i = ; i < n; ++i)
add(i << | , i << , ); s = S << , t = T << | ; printf("%d\n", ans = maxFlow()); for (int k = ; k < n; ++k)if (k != S && k != T)
{
memset(hd, -, sizeof(hd)), edges = ; vis[k] = ; for (int i = ; i < n; ++i)if (!vis[i])
for (int j = ; j < n; ++j)if (!vis[j])
if (G[i][j])add(i << , j << | , inf); for (int i = ; i < n; ++i)if (!vis[i])
add(i << | , i << , ); int flow = maxFlow(); if (ans > flow)
ans = flow, printf("%d ", k + );
else
vis[k] = ;
}
}

@Author: YouSiki

POJ 1815 Friendship的更多相关文章

  1. POJ 1815 Friendship(最小割)

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

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

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

  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 (Dinic 最小割)

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

  5. POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]

    妖怪题目,做到现在:2017/8/19 - 1:41…… 不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√ 题目链接:http://poj.org/problem?id=1815 Tim ...

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

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

  7. POJ 1815 Friendship (Dinic)

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

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

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

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

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

随机推荐

  1. 【工匠大道】 svn命令自己总结

     本文地址   分享提纲: 1. svn 不常见单有用的命令 2. svn查看切换用户 1. svn自己总结的一些不常见,但有用的命令 1)[导出svn不带版本代码]导出不带svn版本控制的代码到本地 ...

  2. Maven部署构件至远程仓库

    私服的一大作用就是部署第三方构件,包括组织内的生成的构件以及一些无法从外部仓库获取的构件.无论是日常开发中生成的构件,还是正式版本发布的构件,都需要部署到仓库中,供其它团队成员使用.Maven除了能对 ...

  3. spring mvc返回json字符串的方式

    spring mvc返回json字符串的方式 方案一:使用@ResponseBody 注解返回响应体 直接将返回值序列化json            优点:不需要自己再处理 步骤一:在spring- ...

  4. Atitit.软件兼容性原理与实践 v5 qa2.docx

    Atitit.软件兼容性原理与实践   v5 qa2.docx 1. Keyword2 2. 提升兼容性的原则2 2.1. What 与how 分离2 2.2. 老人老办法,新人新办法,只新增,少修改 ...

  5. 【代码笔记】iOS-UILable电子表显示

    一,效果图. 二,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIVi ...

  6. JAVA NIO Buffer

    所谓的输入,输出,就是把数据移除或移入缓冲区.   硬件不能直接访问用户控件(JVM). 基于存储的硬件设备操控的是固定大小的数据块儿,用户请求的是任意大小的或非对齐的数据块儿.   虚拟内存:使用虚 ...

  7. Java开发代码性能优化总结

    代码优化,可能说起来一些人觉得没用.可是我觉得应该平时开发过程中,就尽量要求自己,养成良好习惯,一个个小的优化点,积攒起来绝对是有大幅度效率提升的.好了,将平时看到用到总结的分享给大家. 代码优化的目 ...

  8. windows下redis安装

    最近因公司项目原因,去了趟昆明出差,其中第一次接触安装redis,配置sentinel,学习到不少,但也都是皮毛而已,本随笔记下所学知识. 1.首先介绍下redis,来源自百度百科 redis是一个k ...

  9. spring 定时任务@Scheduled

    1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...

  10. Chrome 扩展机制

    据说,今年9月份开始,谷歌将在Chrome浏览器中全面禁用NPAPI插件,Chrome 45以后将无法再加载NPAPI插件,并推出了一种新的机制:扩展. 其实,如果把浏览器看作一块画布的话,NPAPI ...