poj 1815

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<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define  inf 0x0f0f0f0f

using namespace std;

int Map[200+10][200+10],g[200+10][200+10];
int N,S,T;
bool vist[200+10];

struct Edge
{
    int from,to,cap,flow;
};

struct DINIC{
    static const int maxn=2500+10;
    int n,m,s,t;
    vector<Edge>edges;
    vector<int>G[maxn];
    int d[maxn],cur[maxn];
    bool vis[maxn];

    void AddEdge(int from,int to,int cap)
    {
        Edge temp;
        temp.cap=cap; temp.flow=0; temp.from=from; temp.to=to;
        edges.push_back(temp);
        temp.cap=0; temp.flow=0; temp.from=to; temp.to=from;
        edges.push_back(temp);
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BFS()
    {
        memset(vis,0,sizeof(vis));
        queue<int>Q;
        Q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!Q.empty())
        {
            int x=Q.front();Q.pop();
            for (int i=0;i<G[x].size();i++)
            {
                Edge& e=edges[G[x][i]];
                if (!vis[e.to] && e.cap>e.flow)
                {
                    vis[e.to]=1;
                    d[e.to]=d[x]+1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int DFS(int x,int a)
    {
        if (x==t || a==0) return a;
        int flow=0,f;
        for (int& i=cur[x];i<G[x].size();i++)
        {
            Edge& e=edges[G[x][i]];
            if (d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0)
            {
                e.flow+=f;
                edges[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if (a==0) break;
            }
        }
        return flow;
    }

    int Dinic()
    {
        int flow=0;
        while (BFS())
        {
            memset(cur,0,sizeof(cur));
            flow+=DFS(s,inf);
        }
        return flow;
    }

    void init()
    {
        for (int i=0;i<=maxn;i++) G[i].clear();
        edges.clear();
    }
};

DINIC friends;

void build()
{
    friends.init();
    for (int i=1;i<=N;i++)
    {
        if (i==S || i==T)
        {
            friends.AddEdge(i,i+N,inf);
        }
        else friends.AddEdge(i,i+N,1);
        for (int j=1;j<=N;j++)
        {
            if (Map[i][j] && i!=j) friends.AddEdge(i+N,j,inf);
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d%d",&N,&S,&T)!=EOF)
    {
        friends.s=S; friends.t=T+N; friends.n=2*N;
        for (int i=1;i<=N;i++)
        for (int j=1;j<=N;j++){
            scanf("%d",&Map[i][j]);
        }
        if (Map[S][T]==1)
        {
            printf("NO ANSWER!\n");
            continue;
        }
        build();
        int ans=friends.Dinic();
        printf("%d\n",ans);
        memset(vist,0,sizeof(vist));
        for (int i=1;i<=N;i++)
        {
            if (i==S || i==T)
            continue;
            for (int j=1;j<=N;j++)
            for (int k=1;k<=N;k++)
            {
                g[j][k]=Map[j][k];
                if (j==i || k==i) Map[j][k]=0;
            }
            build();
            int ans1=friends.Dinic();
            if (ans1<ans) {
                vist[i]=true;
                ans--;
            }
            else
            {
                for (int j=1;j<=N;j++)
                for (int k=1;k<=N;k++)
                Map[j][k]=g[j][k];
            }
        }
        int k=1;
        for (int i=1;i<=N;i++)
        {
            if (vist[i])
            {
                if (k==1) {printf("%d",i);k++;}
                else printf(" %d",i);
            }
        }
        printf("\n");
    }
    return 0;
}

poj 1815

时间: 2024-10-15 07:46:34

poj 1815的相关文章

POJ 1815 Friendship(最小割)

http://poj.org/problem?id=1815 Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissions: 9026   Accepted: 2534 Description In modern society, each person has his own friends. Since all the people are very busy, they communicate with eac

Poj 1815 Friendship 枚举+求最小割

给以一个图和两个点S,T,问你拿掉最少多少个点可以使得S和T不连通.输出点数并且输出拿掉的是哪些点,如果有多种方法就输出字典序最小的那个. 这就是一个求最小点割集的问题.无向(有向)图G中,给定源点s和终点t,至少要删去多少个点(具体一点,删哪些点),使得s和t不连通.这个问题就是点连通度,也叫最小点割集. 解法其实理解起来不难,只要把图中的每一个点v拆成v',v''两个点,并建立<v',v''>权为1,这样就把最小点割集转化成求最小割的问题. 对于原图的转化也很简单,对于原来的每条边,转化成

POJ 1815 Friendship(最小割)

POJ 1815 Friendship 链接:http://poj.org/problem?id=1815 题目:在现代社会,每个人都有自己的朋友.由于每个人都很忙,他们只通过电话联系.你可以假定A 可以和B 保持联系,当且仅当: (1) A 知道B 的电话号码,或 (2) A 知道C 的号码,而C 能联系上B. 如果A 知道B 的电话号码,则B 也知道A 的电话号码.有时,有人可能会碰到比较糟糕的事情,导致他与其他人失去联系.例如,他可能会丢失了电话簿,或者换了电话号码. 在本题中,告知N 个

poj 1815 Friendship 最小割输出最小方案

这题卡了好久啊,最小割模型很容易想,拆点就行.就像poj的Thieves一样 每个点 a拆成 a->a',容量为1. 其他相连的点 a'->b ,容量为INF 源连接s',t连接汇 问题在于输出最小的割集 更好的方法我还不会,只能枚举. 这里基于贪心的思想,从小到大删边, 若删除i->i',会使得最小割变小,则输出i,并且i->i'这条边不要恢复 若最小割不变,则恢复这条边,继续枚举. 一开始就是因为恢复了要割去的边,无限WA. #include<cstdio> #in

poj 1815 Friendship (最小割+拆点+枚举)

题意: 就在一个给定的无向图中至少应该去掉几个顶点才能使得s和t不联通. 算法: 如果s和t直接相连输出no answer. 把每个点拆成两个点v和v'',这两个点之间连一条权值为1的边(残余容量) v和v''分别是一个流进的点,一个流出的点. 根据求最小割的性质,权值小的边是可能被选择的(断开的). 添加源点st=0和汇点en=2*n+1,源点与s连权值为inf的边,t''与汇点连权值为inf的边. s与s'',t与t''连权值为inf的边,这样保证自己和自己是不会失去联系的. 如果i和j有边

poj 1815 Friendship 字典序最小+最小割

题目链接:http://poj.org/problem?id=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 kno

POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]

妖怪题目,做到现在:2017/8/19 - 1:41-- 不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√ 题目链接:http://poj.org/problem?id=1815 Time Limit: 2000MS Memory Limit: 20000K Description In modern society, each person has his own friends. Since all the people are very busy, they communic

POJ 1815 Friendship(最小割+字典序输出割点)

http://poj.org/problem?id=1815 题意: 在现代社会,每个人都有自己的朋友.由于每个人都很忙,他们只通过电话联系.你可以假定A可以和B保持联系,当且仅当:①A知道B的电话号码:②A知道C的电话号码,而C能联系上B.如果A知道B的电话号码,则B也知道A的电话号码. 思路:这题是要我们删点,既然是删点,那么就要拆点,容量就是1. 接下来凡是能联系的,就连边,容量为INF,因为我们不是要删除这些边.跑遍最大流就能算出至少要删除多少个点. 这道题的关键是要字典序顺序输出最小割

POJ 1815 Friendship

Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissions: 10614   Accepted: 2945 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