POJ 1087 A Plug for UNIX(最大二分匹配)

来源:互联网 发布:华通云数据 编辑:程序博客网 时间:2024/05/22 14:31
A Plug for UNIX
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12051 Accepted: 3972

Description

You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an international mandate to make the free flow of information and ideas on the Internet as cumbersome and bureaucratic as possible. 
Since the room was designed to accommodate reporters and journalists from around the world, it is equipped with electrical receptacles to suit the different shapes of plugs and voltages used by appliances in all of the countries that existed when the room was built. Unfortunately, the room was built many years ago when reporters used very few electric and electronic devices and is equipped with only one receptacle of each type. These days, like everyone else, reporters require many such devices to do their jobs: laptops, cell phones, tape recorders, pagers, coffee pots, microwave ovens, blow dryers, curling 
irons, tooth brushes, etc. Naturally, many of these devices can operate on batteries, but since the meeting is likely to be long and tedious, you want to be able to plug in as many as you can. 
Before the meeting begins, you gather up all the devices that the reporters would like to use, and attempt to set them up. You notice that some of the devices use plugs for which there is no receptacle. You wonder if these devices are from countries that didn't exist when the room was built. For some receptacles, there are several devices that use the corresponding plug. For other receptacles, there are no devices that use the corresponding plug. 
In order to try to solve the problem you visit a nearby parts supply store. The store sells adapters that allow one type of plug to be used in a different type of outlet. Moreover, adapters are allowed to be plugged into other adapters. The store does not have adapters for all possible combinations of plugs and receptacles, but there is essentially an unlimited supply of the ones they do have.

Input

The input will consist of one case. The first line contains a single positive integer n (1 <= n <= 100) indicating the number of receptacles in the room. The next n lines list the receptacle types found in the room. Each receptacle type consists of a string of at most 24 alphanumeric characters. The next line contains a single positive integer m (1 <= m <= 100) indicating the number of devices you would like to plug in. Each of the next m lines lists the name of a device followed by the type of plug it uses (which is identical to the type of receptacle it requires). A device name is a string of at most 24 alphanumeric 
characters. No two devices will have exactly the same name. The plug type is separated from the device name by a space. The next line contains a single positive integer k (1 <= k <= 100) indicating the number of different varieties of adapters that are available. Each of the next k lines describes a variety of adapter, giving the type of receptacle provided by the adapter, followed by a space, followed by the type of plug.

Output

A line containing a single non-negative integer indicating the smallest number of devices that cannot be plugged in.

Sample Input

4 A B C D 5 laptop B phone C pager B clock B comb X 3 B X X A X D 

Sample Output

1

Source

East Central North America 1999 
解题思路:很明显的二分匹配,这里用Dinic求最大流来求最大二分匹配。
建模:设S和T点,从S到每个device(即用电设备),这里每种device只有一个,所以连边的权值为1;对于每个device,都有与之相对应的plug(及插头),所以从device到对应的插头连接一条权值为1的边;对于adapter,将其与plug有关的边设为无穷大(题目中已给出adapter的数量没有限制);最后从每种plug到T连接一条权值为flag[plug's id]的边(flag[]存放每种plug的个数,这里我视plug每种不只为一个)。
#include <cstdio>#include <iostream>#include <cstring>using namespace std;const int mm=20000;const int mn=1000;const int oo=1000000000;bool map[mn][mn];int node,s,t,edge;int to[mm],flow[mm],next[mm];int head[mn],work[mn],dis[mn],q[mn];int id_num,flag[mn],device[mn],plu[mn];bool adap[mn],dev[mn];char list[mn][30];inline int min(int a,int b){    return a<b?a:b;}int getId(char s[]){    for(int i=1; i<=id_num; i++)        if(strcmp(s,list[i])==0) return i;    strcpy(list[++id_num],s);    return id_num;}inline void init(int nn,int ss,int tt){    node=nn,s=ss,t=tt,edge=0;    for(int i=0; i<node; ++i) head[i]=-1;}inline void add(int u,int v,int c){    to[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;    to[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;}bool bfs(){    int i,u,v,l,r=0;    for(i=0; i<node; ++i) dis[i]=-1;    dis[q[r++]=s]=0;    for(l=0; l<r; ++l)        for(i=head[u=q[l]]; i>=0; i=next[i])            if(flow[i]&&dis[v=to[i]]<0)            {                dis[q[r++]=v]=dis[u]+1;                if(v==t)return 1;            }    return 0;}int dfs(int u,int maxf){    if(u==t) return maxf;    for(int &i=work[u],v,tmp; i>=0; i=next[i])        if(flow[i]&&dis[v=to[i]]==dis[u]+1&&(tmp=dfs(v,min(maxf,flow[i])))>0)        {            flow[i]-=tmp;            flow[i^1]+=tmp;            return tmp;        }    return 0;}int dinic(){    int i,ret=0,delta;    while(bfs())    {        for(i=0; i<node; ++i) work[i]=head[i];        while(delta=dfs(s,oo)) ret+=delta;    }    return ret;}int main(){    int i,j,n,m,k,at,bt,plug_num=0,dev_num=0;    char a[30],b[30];    memset(adap,0,sizeof(adap));    memset(flag,0,sizeof(flag));    memset(map,0,sizeof(map));    id_num=0;    cin>>n;    for(i=0; i<n; i++)    {        cin>>a;        flag[at=getId(a)]++;        if(flag[at]==1) plu[plug_num++]=at;    }    cin>>m;    for(i=0; i<m; i++)    {        cin>>a>>b;        map[at=getId(a)][bt=getId(b)]=1;        device[dev_num++]=at;        dev[at]=1;        if(flag[bt]==0)            adap[bt]=1;    }    cin>>k;    for(i=0; i<k; i++)    {        cin>>a>>b;        map[at=getId(a)][bt=getId(b)]=1;        if(!flag[at]) adap[at]=1;        if(!flag[bt]) adap[bt]=1;    }    init(id_num+2,0,id_num+1);    for(i=1; i<=id_num; ++i)        for(j=1; j<=id_num; ++j)        {            if(map[i][j])            {                if(!dev[i]&&!dev[j]) add(i,j,oo);                else                    add(i,j,1);            }        }    for(i=0; i<dev_num; i++)        add(s,device[i],1);    for(i=0; i<plug_num; i++)        add(plu[i],t,flag[plu[i]]);    printf("%d\n",m-dinic());    return 0;}


原创粉丝点击