D - Friendship POJ - 1815 (网络流+拆点建边)

本文探讨了POJ-1815 D-Friendship问题,通过构建特殊图模型使用最大流算法解决至少需要移除多少节点使得特定两点失去联系的问题,并提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D - Friendship

 POJ - 1815 

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之间不再连通,把点v拆成两点v'和v'',让连接v'和v''的边的权值为1,那么以s为源点,t为汇点,找到的最大流就是要拆的最少点数,因为任何两个增广路径不会共用一个结点(这一点需要好好体会),因此拆掉一个点最多使s与t之间的流量减少1。从小到大枚举拆点。(对于可以不可以不拆点建边的问题:这是无向边,正向边的权值和反向边的权值相等,建邻接表时ad()会把反向边权值置0,再者如果在下面ad()函数中不把反向边置0,在下面代码(1)处找反向边会很麻烦,所以不拆点会带来一系列的麻烦)

#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=444;
const int mmax = 200000+ 7;
int s,t,n;
int str[222][222],g[222][222],head[N],cur[N],dep[N],judge[222];
struct node
{
    int v,w,next;
} gra[mmax];
int num;
void ad(int u,int v,int w)
{
    gra[num].v=v;
    gra[num].next=head[u];
    gra[num].w=w;
    head[u]=num++;
    gra[num].next=head[v];
    gra[num].v=u;
    gra[num].w=0;
    head[v]=num++;
}
int bfs()
{
    queue<int>q;
    memset(dep,0,sizeof(dep));
    q.push(s);
    dep[s]=1;
    while(!q.empty())
    {
        int k=q.front();
        q.pop();
        if(k==t+n)
            return 1;
        for(int i=head[k]; i!=-1; i=gra[i].next)
        {
            if(!dep[gra[i].v]&&gra[i].w>0)
            {
                dep[gra[i].v]=dep[k]+1;
                q.push(gra[i].v);
            }
        }
    }
    return 0;
}
int dfs(int now,int minl)
{
    if(now==t+n)
        return minl;
    for(int &i=cur[now]; i!=-1; i=gra[i].next)
    {
        if(dep[gra[i].v]==dep[now]+1&&gra[i].w>0)
        {
            int tminl=dfs(gra[i].v,min(gra[i].w,minl));
            if(tminl>0)
            {
                gra[i].w-=tminl;
                gra[i^1].w+=tminl;   //(1)
                return tminl;
            }
        }
    }
    return 0;
}
int Dinic()
{
    int maxl=0;
    while(bfs())
    {
        for(int i=1; i<=2*n; i++)
            cur[i]=head[i];
        while(int flow=dfs(s,inf))
            maxl+=flow;
    }
    return maxl;
}
void build()
{
    num=0;
    memset(head,-1,sizeof(head));
    for(int i=1; i<=n; i++)
    {
        if(i!=s&&i!=t)
            ad(i,i+n,1);
        else
            ad(i,i+n,inf);
        for(int j=1; j<=n; j++)
        {
            if(i!=j&&str[i][j])
            {
                ad(i+n,j,1);
            }
        }
    }
}
int main()
{
    int ans;
    while(scanf("%d%d%d",&n,&s,&t)!=EOF)
    {
        memset(judge,0,sizeof(judge));
        int k=0;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                scanf("%d",&str[i][j]);
            }
        if(str[s][t]==1)
        {
            printf("NO ANSWER!");
            continue;
        }
        build();
        ans=Dinic();
        printf("%d\n",ans);
        for(int i=1; i<=n; i++)
        {
            if(i==s||i==t)
                continue;
            for(int j=1; j<=n; j++)
            {
                for(int u=1; u<=n; u++)
                {
                    g[j][u]=str[j][u];
                    if(j==i||u==i)
                        str[j][u]=0;
                }
            }
            build();
            int sum=Dinic();
            if(sum<ans)
            {
                ans--;
                k++;
                judge[i]=1;
            }
            else
            {
                for(int j=1; j<=n; j++)
                {
                    for(int u=1; u<=n; u++)
                    {
                        str[j][u]=g[j][u];
                    }
                }
            }

        }

        for(int i=1; i<=n; i++)
        {
            if(judge[i])
            {
                k--;
                printf("%d%c",i,k==0?'\n':' ');
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值