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

本文概述了AI音视频处理领域的关键技术,包括视频分割、语义识别、自动驾驶、AR、SLAM、物体检测与识别、语音识别变声等,探讨了AI在音视频处理的应用与发展趋势。

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

A Plug for UNIX
Time Limit: 1000MS Memory Limit: 65536K
Total 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

解题思路:很明显的二分匹配,这里用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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值