Friendship
Time Limit: 2000MS | Memory Limit: 20000K | |
Total Submissions: 8465 | Accepted: 2370 |
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.
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.
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.
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
题意:给出一个无向图,给出起点S和终点T,求最小删除多少个点,能使得S不能到达T,并且输出删除的点,若有多种方案,则输出字典序最小的。
思路:求最小点割集。做法是:将每个点拆点为i和i',i和i'之间连边容量为1(对于S和T,容量为INF),则转化为求点不相交的路径的条数。
对于原图中的边(u,v),则连边u+n->v,容量为INF,然后求最大流,就得到最小割的数目,即第一个输出的答案。
对于输出字典序最小的点,可以从1~n枚举点,删除i和i'之间的边,求最大流,若求得的最大流小于未删边时的最大流,则这个点在最小点割集中,否则,将该边还原。
AC代码:
#include <iostream> #include <cmath> #include <cstdlib> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <ctime> #include <algorithm> #define ll __int64 using namespace std; const int INF = 1000000000; const int maxn = 1000; struct Edge{ int u, v, cap, flow, next; }et[maxn * maxn]; int low[maxn], pre[maxn], cnt[maxn], dis[maxn], cur[maxn], eh[maxn]; int s, t, num, n, ss, tt; void init(){ memset(eh, -1, sizeof(eh)); num = 0; } void add(int u, int v, int cap, int flow){ Edge e = {u, v, cap, flow, eh[u]}; et[num] = e; eh[u] = num++; } void addedge(int u, int v, int cap){ add(u, v, cap, 0); add(v, u, 0, 0); } int isap(int s, int t, int nv){ int u, v, now, flow = 0; memset(low, 0, sizeof(low)); memset(cnt, 0, sizeof(cnt)); memset(dis, 0, sizeof(dis)); for(u = 0; u <= nv; u++) cur[u] = eh[u]; low[s] = INF, cnt[0] = nv, u = s; while(dis[s] < nv) { for(now = cur[u]; now != -1; now = et[now].next) if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break; if(now != -1) { cur[u] = pre[v] = now; low[v] = min(low[u], et[now].cap - et[now].flow); u = v; if(u == t) { for(; u != s; u = et[pre[u]].u) { et[pre[u]].flow += low[t]; et[pre[u]^1].flow -= low[t]; } flow += low[t]; low[s] = INF; } } else { if(--cnt[dis[u]] == 0) break; dis[u] = nv, cur[u] = eh[u]; for(now = eh[u]; now != -1; now = et[now].next) if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1) dis[u] = dis[et[now].v] + 1; cnt[dis[u]]++; if(u != s) u = et[pre[u]].u; } } return flow; } int main() { int a; while(~scanf("%d%d%d", &n, &ss, &tt)) { init(); s = 0, t = 2 * n + 1; for(int i = 1; i <= n; i++) { if(i == ss || i == tt) addedge(i, i + n, INF); else addedge(i, i + n, 1); } bool flag = true; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) { scanf("%d", &a); if(i == j) continue; if(a) { if(i == ss && j == tt) flag = false; addedge(i + n, j, INF); } } if(!flag) { printf("NO ANSWER!\n"); continue; } addedge(s, ss, INF); addedge(tt + n, t, INF); int maxflow = isap(s, t, t + 1); int ans[maxn], tot = 0, e; for(int i = 1; i <= n; i++) { if(i == ss || i == tt) continue; for(int j = 0; j < num; j++) { et[j].flow = 0; if(et[j].u == i && et[j].v == i + n) et[e = j].cap = 0; } int tmp = isap(s, t, t + 1); if(tmp < maxflow) { ans[tot++] = i; maxflow--; } else et[e].cap = 1; if(maxflow <= 0) break; } printf("%d\n", tot); for(int i = 0; i < tot; i++) { if(i < tot - 1) printf("%d ", ans[i]); else printf("%d", ans[i]); } puts(""); } return 0; }