zoukankan      html  css  js  c++  java
  • 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 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
    

    Source

    分析:

    还是无向图的点连通度,这次确定了S和T,但是要求输出字典序最小的方案,所以还是得枚举TAT...

    我们发现,如果去掉最小割集合中的一条边,那么最大流一定减少了这条边的容量值...所以我们从小到大枚举点然后在图中删去这个点拆成的边,如果发现这个点对应的边是最小割中的边,那么就要把这个点永远删去,否则还要还原...

    代码:

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdio>
     5 //by NeighThorn
     6 #define inf 0x3f3f3f3f
     7 using namespace std;
     8 //大鹏一日同风起,扶摇直上九万里 
     9 
    10 const int maxn=400+5,maxm=5000*2+400*400*2+5;
    11 
    12 int n,S,T,ans,cnt,hd[maxn],to[maxm],fl[maxm],nxt[maxm],pos[maxn],vis[maxn],mp[maxn][maxn];
    13 
    14 inline void add(int s,int x,int y){
    15     fl[cnt]=s;to[cnt]=y;nxt[cnt]=hd[x];hd[x]=cnt++;
    16     fl[cnt]=0;to[cnt]=x;nxt[cnt]=hd[y];hd[y]=cnt++;
    17 }
    18 
    19 inline bool bfs(void){
    20     memset(pos,-1,sizeof(pos));
    21     int head=0,tail=0,q[maxn];
    22     q[0]=S,pos[S]=0;
    23     while(head<=tail){
    24         int top=q[head++];
    25         for(int i=hd[top];i!=-1;i=nxt[i])
    26             if(pos[to[i]]==-1&&fl[i])
    27                 pos[to[i]]=pos[top]+1,q[++tail]=to[i];
    28     }
    29     return pos[T]!=-1;
    30 }
    31 
    32 inline int find(int v,int f){
    33     if(v==T)
    34         return f;
    35     int res=0,t;
    36     for(int i=hd[v];i!=-1&&f>res;i=nxt[i])
    37         if(pos[to[i]]==pos[v]+1&&fl[i])
    38             t=find(to[i],min(fl[i],f-res)),fl[i]-=t,fl[i^1]+=t,res+=t;
    39     if(!res)
    40         pos[v]=-1;
    41     return res;
    42 }
    43 
    44 inline int dinic(void){
    45     int res=0,t;
    46     while(bfs())
    47         while(t=find(S,inf))
    48             res+=t;
    49     return res;
    50 }
    51 
    52 signed main(void){
    53     memset(hd,-1,sizeof(hd));
    54     memset(vis,0,sizeof(vis));
    55     scanf("%d%d%d",&n,&S,&T);cnt=0;
    56     for(int i=1;i<=n;i++)
    57         for(int j=1;j<=n;j++)
    58             scanf("%d",&mp[i][j]);
    59     for(int i=1;i<=n;i++)
    60         add(1,i,i+n);
    61     for(int i=1;i<=n;i++)
    62         for(int j=1;j<=n;j++)
    63             if(i!=j&&mp[i][j])
    64                 add(inf,i+n,j);
    65     S+=n;ans=dinic();
    66     if(ans==inf){
    67         puts("NO ANSWER!");
    68         return 0;
    69     }
    70     printf("%d
    ",ans);
    71     for(int i=1;i<=n;i++)
    72         if(i!=S&&i!=T){
    73             for(int j=0;j<cnt;j+=2)
    74                 fl[j]+=fl[j^1],fl[j^1]=0;
    75             for(int j=0;j<cnt;j+=2)
    76                 if(to[j]==i+n&&to[j^1]==i)
    77                     fl[j]=0;
    78             if(dinic()==ans-1)
    79                 ans--,vis[i]=1;
    80             else
    81                 for(int j=0;j<cnt;j+=2)
    82                     if(to[j]==i+n&&to[j^1]==i)
    83                         fl[j]=1,fl[j^1]=0;
    84         }
    85     for(int i=1;i<=n;i++)
    86         if(vis[i])
    87             printf("%d ",i);
    88     puts("");
    89     return 0;
    90 }//Cap ou pas cap. Cap.
    View Code

    By NeighThorn

  • 相关阅读:
    关于Maya Viewport 2.0 API 开发的介绍视频
    春节大假
    Some tips about the life cycle of Maya thread pool
    Can I compile and run Dx11Shader for Maya 2015 on my side?
    How to get current deformed vertex positions in MoBu?
    想加入全球首届的 欧特克云加速计划吗?
    三本毕业(非科班),四次阿里巴巴面试,终拿 offer(大厂面经)
    mac、window版编辑器 webstorm 2016... 永久破解方法。
    node 搭载本地代理,处理web本地开发跨域问题
    js 一维数组,转成嵌套数组
  • 原文地址:https://www.cnblogs.com/neighthorn/p/6229895.html
Copyright © 2011-2022 走看看