poj1815 Friendship *[最小割点集(拆点)]

in 题目 with 0 comment

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
  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 , S and 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 \le A1 < A2 <...< At \le N )$, the score will be $(A_1-1)N^t+(A_2-1)N^(t-1)+...+(A_t-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


Hint

$2 \le N \le 200 $
$ 1 \le S, T \le N $ and S is not equal to T


Source

POJ Monthly


Solution

这道题是一道最小点割集的题,建图方法很简单,我们把每个点i拆成 i , i+n ,若 i , j 连通 , $i+n\rightarrow j$ 连边,权值为$\infty$,另外 ,我们还要连 $ i\rightarrow i+n $ ,权值为1,因为每删一个点就能对应到割掉 $i\rightarrow i+n$ 间的一条边.利用最大流我们可以很容易的求出至少要删几个点.怎么输出字典序最小的删点方案呢?

我们可以考虑从小到大枚举每一个点,如果这个点被删掉,那么mp[ ][ ]中原来的mp[ i ][ j ]与mp[ j ][ i ] 将全部变为0 ,我们可以再开两个数组记录这一行一列的信息,以保证如果这个点不符合要求时可以将图恢复原样 . 我们对每个点跑一次最大流,如果需要删掉的点数小于目前剩余需要删掉的点数,说明删这个点符合要求,记录到答案中即可!


Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#define maxn 510
#define maxe 50010
#define INF 0x3f3f3f3f
using namespace std;
int N,S,T;
int num_e,head[maxn],dis[maxn];
int mp[maxn][maxn];
int map1[maxn],map2[maxn],res[maxn];

struct edge 
{
    int to,next,val;
}e[maxe];

inline int read()
{
    char ch;
    int read=0,sign=1;
    do
        ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-');
    if(ch=='-') sign=-1,ch=getchar();
    while(ch>='0'&&ch<='9')
    {
        read=read*10+ch-'0';
        ch=getchar();
    }
    return read*sign;
}

inline void ins(int u,int v,int w)
{
    num_e++;
    e[num_e].to=v;
    e[num_e].next=head[u];
    e[num_e].val=w;
    head[u]=num_e;
}

inline void insert(int u,int v,int w)
{ ins(u,v,w);ins(v,u,0); }

void init()
{
    memset(mp,0,sizeof(mp));
    for(int i=1;i<=N;++i)
        for(int j=1;j<=N;++j)
            mp[i][j]=read();
}

void build_graph()
{
    num_e=1;
    memset(e,0,sizeof(e));
    memset(head,0,sizeof(head));
    for(int i=1;i<=N;++i)
        insert(i,i+N,1);
    for(int i=1;i<=N;++i)
        for(int j=1;j<=N;++j)
            if(i!=j && mp[i][j]==1)
                insert(i+N,j,INF);
}

bool bfs()
{
    memset(dis,-1,sizeof(dis));
    queue<int> q;
    q.push(S+N);
    dis[S+N]=0;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        int i=head[u];
        while(i)
        {
            if(e[i].val && dis[e[i].to]<0)
            {
                dis[e[i].to]=dis[u]+1;
                q.push(e[i].to); 
            }
            i=e[i].next;
        }
    }
    if(dis[T]==-1) return 0;
    return 1;
}

int dfs(int x,int flow)
{
    if(x==T) return flow;
    int used=0,i=head[x],w;
    while(i)
    {
        if(e[i].val && dis[e[i].to]==dis[x]+1)
        {
            w=flow-used;
            w=dfs(e[i].to,min(w,e[i].val));
            e[i].val-=w;
            e[i^1].val+=w;
            used+=w;
            if(used==flow) return flow;
        }
        i=e[i].next;
    }
    if(!used) dis[x]=-1;
    return used;
}

int dinic()
{
    int ans=0;
    while(bfs()) ans+=dfs(S+N,INF);
    return ans;
}

void print_nodes()
{
    int ans=dinic();
    cout<<ans<<endl;
    int cnt=0,max_flow=ans;
    for(int i=1;i<=N;++i)
    {
        if(i==S||i==T) continue;
        for(int j=1;j<=N;++j)
        {
            map1[j]=mp[i][j];
            map2[j]=mp[j][i];
            mp[i][j]=mp[j][i]=0;
        }
        build_graph();
        int k=dinic();
        if(max_flow>k)
        {
            res[++cnt]=i;
            max_flow--;
        }
        else
        {
            for(int j=1;j<=N;++j)
            {
                mp[i][j]=map1[j];
                mp[j][i]=map2[j];
            }
        }
        if(max_flow==0) break;
    }
    for(int i=1;i<=cnt;++i) cout<<res[i]<<" ";
    cout<<endl;
}

int main()
{
    while(scanf("%d%d%d",&N,&S,&T)==3)
    {
        init();
        if(mp[S][T]!=0)
        {
            printf("NO ANSWER!\n");
            return 0;
        }
        build_graph();
        print_nodes();
    }
    return 0;
} 

扫描二维码,在手机上阅读!
Responses