POJ 1815 Friendship (最小点割集)

来源:互联网 发布:旧版淘宝下载2015 编辑:程序博客网 时间:2024/06/06 23:53
Friendship
Time Limit: 2000MSMemory Limit: 20000KTotal Submissions: 7888Accepted: 2188

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 31 1 01 1 10 1 1

Sample Output

12最小点割集:删掉最少的点数,使得源点S和汇点T不连通。一般做法是:拆点,把点v拆成v和v',连权值为1的边,对于原来的u-v,连边u'-v,权值为无穷大。同时网络中源点为S',汇点为T。
题目要求字典序输出,所以从小到大枚举每个点,删除当前点,重新构图,看最大流跑出的结果是否比上一次小,如果是,则说明这个点是点割集里的点,否则还原该点,继续往下找。
PS:
如果不要求字典序,则只需把残留网络中的流量为0(满流)的边的权值改为1,流量不为0的改为无穷,再跑一遍最大流,就可得到割边的数量,最后枚举每条边,若流量为0(满流)则为点割集中的点。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define SIZE 512#define inf 0xfffffffusing namespace std;struct node{    int to,val,next;}edge[SIZE*SIZE];int N,S,T,sc,sk,pt;int head[SIZE],idx;int gap[SIZE],dis[SIZE];int cnt[SIZE>>1][SIZE>>1];bool cut[SIZE>>1];int ans[SIZE>>1],num;void addnode(int from,int to,int val){    edge[idx].to = to;    edge[idx].val = val;    edge[idx].next = head[from];    head[from] = idx ++;    edge[idx].to = from;    edge[idx].val = 0;    edge[idx].next = head[to];    head[to] = idx ++;}int dfs(int cur,int cval){    if(cur == sk)        return cval;    int mindis = pt-1, tval = cval;    for(int i=head[cur]; i!=-1; i=edge[i].next)    {        int to = edge[i].to;        if(edge[i].val > 0)        {            if(dis[to] + 1 == dis[cur])            {                int val = dfs(to,min(edge[i].val,tval));                tval -= val;                edge[i].val -= val;                edge[i^1].val += val;                if(dis[sc] >= pt)                    return cval - tval;                if(!tval)                    break;            }            if(mindis > dis[to])                mindis = dis[to];        }    }    if(cval == tval)    {        --gap[dis[cur]];        if(!gap[dis[cur]])            dis[sc] = pt;        dis[cur] = mindis+1;        ++gap[dis[cur]];    }    return cval - tval;}int sap(){    memset(gap,0,sizeof(gap));    memset(dis,0,sizeof(dis));    int ret = 0;    gap[sc] = pt;    while(dis[sc] < pt)        ret += dfs(sc,inf);    return ret;}void construct(){    idx = 0;    memset(head,-1,sizeof(head));    sc = S+N, sk = T, pt = 2*N+1;    for(int i=1; i<=N; i++)        if(!cut[i])            addnode(i,i+N,1);    for(int i=1; i<=N; i++)    {        for(int j=1; j<=N; j++)        {            if(cnt[i][j])            {                addnode(i+N,j,inf);                addnode(j+N,i,inf);            }        }    }}int main(){    scanf("%d%d%d",&N,&S,&T);    memset(cut,0,sizeof(cut));    for(int i=1; i<=N; i++)        for(int j=1; j<=N; j++)            scanf("%d",&cnt[i][j]);    if(cnt[S][T])    {        puts("NO ANSWER!");        return 0;    }    construct();    int res = sap();    memset(ans,0,sizeof(ans));    num = 0;    for(int i=1; i<=N; i++)    {        if(i == S || i == T)            continue;        cut[i] = true;        construct();        if(sap() < res)        {            ans[++num] = i;            res --;        }        else            cut[i] = false;        if(!res)            break;    }    printf("%d\n",num);    for(int i=1; i<=num; i++)        printf("%d ",ans[i]);    return 0;}

 

 

	
				
		
原创粉丝点击