poj 1815 Friendship 最小割+邻接表 求最小点割集(求无向图中最少删除几个顶点使得s和t不联通) 输出顶点是要枚举

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

 

//

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
const int inf=(1<<28);
using namespace std;
#define maxn 5000
struct edge
{
    int u,v,next,f,pre;
    int flow;
}e[2*maxn];//maxn不能开太大 否则超时
int num,rnum;
int head[maxn],rhead[maxn];
int d[maxn];
int numb[maxn];
int start[maxn];
int n;//见图后的点数(1->n)
int p[maxn];
int source,sink;
//要初始化source 和 sink,重定义n
void Init()
{
    memset(head,-1,sizeof(head));
    memset(rhead,-1,sizeof(rhead));
    memset(p,-1,sizeof(p));
    num=0;
    return ;
}
void BFS()
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        d[i]=n;
        numb[i]=0;
    }
    int Q[maxn],head(0),tail(0);
    d[sink]=0;
    numb[0]=1;
    Q[++tail]=sink;
    while(head<tail)
    {
        i=Q[++head];
        for(j=rhead[i];j!=-1;j=e[j].pre)
        {
            if(e[j].f==0||d[e[j].u]<n)
                continue;
            d[e[j].u]=d[i]+1;
            numb[d[e[j].u]]++;
            Q[++tail]=e[j].u;
        }
    }
    return ;
}
int Augment()
{
    int i;
    int tmp=inf;
    for(i=p[sink];i!=-1;i=p[e[i].u])
    {
        if(tmp>e[i].f)
            tmp=e[i].f;
    }
    for(i=p[sink];i!=-1;i=p[e[i].u])
    {
        e[i].f-=tmp;e[i].flow+=tmp;
        e[i^1].f+=tmp;e[i^1].flow-=tmp;
    }
    return tmp;
}
int Retreat(int &i)
{
    int tmp,j,mind(n-1);
    for(j=head[i];j!=-1;j=e[j].next)
    {
        if(e[j].f>0&&d[e[j].v]<mind)
            mind=d[e[j].v];
    }
    tmp=d[i];
    d[i]=mind+1;
    numb[tmp]--;
    numb[d[i]]++;
    if(i!=source)
        i=e[p[i]].u;
    return numb[tmp];
}
int maxflow()
{
    int flow(0),i,j;
    BFS();
    for(i=1;i<=n;i++)
        start[i]=head[i];
    i=source;
    while(d[source]<n)
    {
        for(j=start[i];j!=-1;j=e[j].next)
            if(e[j].f>0&&d[i]==d[e[j].v]+1)
                break;
        if(j!=-1)
        {
            start[i]=j;
            p[e[j].v]=j;
            i=e[j].v;
            if(i==sink)
            {
                flow+=Augment();
                i=source;
            }
        }
        else
        {
            start[i]=head[i];
            if(Retreat(i)==0)
                break;
        }
    }
    return flow;
}
//a->b=c;
void addedge(int a,int b,int c)
{
    e[num].next=head[a];
    e[num].flow=0;
    head[a]=num;
    e[num].pre=rhead[b];
    rhead[b]=num;
    e[num].f=c;
    e[num].u=a;
    e[num++].v=b;
    e[num].next=head[b];
    e[num].flow=0;
    head[b]=num;
    e[num].pre=rhead[a];
    rhead[a]=num;
    e[num].u=b;
    e[num].v=a;
    e[num++].f=0;
    return ;
}
int mat[300][300];
int main()
{
    int m,s,t;
    while(scanf("%d%d%d",&m,&s,&t)==3)
    {
        Init();
        n=m*2;
        source=s;sink=t+m;
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&mat[i][j]);
                if(i==j&&mat[i][j])
                {
                    if(i==s||i==t) addedge(i,i+m,inf);
                    else addedge(i,i+m,1);
                }
                else if(i!=j&&mat[i][j])
                {
                    addedge(i+m,j,inf);
                }
            }
        }
        if(mat[s][t]) {printf("NO ANSWER!\n");continue;}
        int cnt=maxflow();
        printf("%d\n",cnt);
        int ans[1000];int ln=0;
        //枚举顶点
        for(int l=1;l<=m;l++)
        {
            if(l==s||l==t) continue;
            Init();
            for(int i=1;i<=m;i++) mat[l][i]=-mat[l][i],mat[i][l]=-mat[i][l];
            for(int i=1;i<=m;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    if(i==j&&mat[i][j]>0)
                    {
                        if(i==s||i==t) addedge(i,i+m,inf);
                        else addedge(i,i+m,1);
                    }
                    else if(i!=j&&mat[i][j]>0)
                    {
                        addedge(i+m,j,inf);
                    }
                }
            }
            int tmp=maxflow();
            if(tmp<cnt) ans[ln++]=l,cnt--;
            else for(int i=1;i<=m;i++) mat[l][i]=-mat[l][i],mat[i][l]=-mat[i][l];
        }
        if(ln)
        {
            printf("%d",ans[0]);
            for(int i=1;i<ln;i++) printf(" %d",ans[i]);printf("\n");
        }
    }
    return 0;
}
/*
9 1 9
1 1 1 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0
1 1 1 0 1 1 0 0 0
0 1 0 1 0 0 1 0 0
0 1 1 0 1 0 1 1 0
0 0 1 0 0 1 0 1 0
0 0 0 1 1 0 1 1 1
0 0 0 0 1 1 1 1 1
0 0 0 0 0 0 1 1 1

4 1 4
1 1 1 0
1 1 0 1
1 0 1 1
0 1 1 1
*/

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值