1. 程式人生 > >poj 1815 Friendship(最小點割集)

poj 1815 Friendship(最小點割集)

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.

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
題意:給出一個無向圖,給出起點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;
}