题解:

网络流裸题

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=405;
int q[N*N],n,m,sum,dis[N],x,y,ans,f[N],z,a[N][N];
int bfs()
{
    memset(dis,0xff,sizeof dis);
    dis[1]=0;
    int l=0,r=1;
    q[1]=1;
    while (l<r)
     {
         int j=q[++l];
         for (int i=1;i<=n;i++)
          if (dis[i]<0&&a[j][i]>0)
           {
               dis[i]=dis[j]+1;
               q[++r]=i;
           }
     }
    if (dis[n]>0)return 1;
    return 0; 
}
int find(int x,int low)
{
    int b=0;
    if (x==n)return low;
    for (int i=1;i<=n;i++)
     if (a[x][i]>0&&dis[i]==dis[x]+1&&(b=find(i,min(low,a[x][i]))))
      {
          a[x][i]-=b;
          a[i][x]+=b;
          return b;
      }
    return 0;  
}
int main()
{
    scanf("%d",&n);
    memset(a,0,sizeof a);
    for (int i=1;i<n;i++)
     {
         scanf("%d",&m);
        while (m--)
          {
             scanf("%d",&x);
             if (i==1||x==n)a[i][x]+=1;
             else a[i][x]=1e9;
         }
     }
    ans=0;int t; 
    while (bfs())
     while (t=find(1,0x7fffffff))ans+=t;
    printf("%d\n",ans); 
}

 

posted on 2018-01-28 16:32  宣毅鸣  阅读(129)  评论(0编辑  收藏  举报