poj 1087 a plug for unix

题目:
A Plug for UNIX
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10861 Accepted: 3562
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








laptop B 
phone C 
pager B 
clock B 
comb X 

B X 
X A 
X D 
Sample Output


1
Source


East Central North America 1999




题意:
输入m表示插座数量,然后是m个字符串表示每个插座
输入n表示设备数量,然后是n行每行两个字符串,表示设备名称和它的插头,同样的插头可以插到同样的插座上
输入k表示转换器的数量,然后是k行每行两个字符串,表示该转换器能将需要前一个字符串表示的插头的设备插入到后一个字符串表示的插座中
问最少有多少设备没有插座用,转换器数量不限,作用可传递,即一个转换器不够用可以通过转换器接转换器来转换。


代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<string>
using namespace std;


int m,n,k;


int a[105][105];
int a1[105];
int a2[105];


int g[405][405];//表示设备与插座间的可插入关系


bool fl[105];


string plug[105];//表示每个插座
string dev[105];//表示每个设备需要的插座


void floyd(int cnt)
{
for(int k=1;k<=cnt;k++)
{
for(int i=1;i<=cnt;i++)
{
for(int j=1;j<=cnt;j++)
{
if(g[i][k]==1&&g[k][j]==1)
{
g[i][j]=1;
}
}
}
}
}


bool search(int v)
{
for(int i=1;i<=m;i++)
{
if(a[v][i]&&fl[i]==0)
{
fl[i]=1;
if(a2[i]==-1||search(a2[i]))
{
a2[i]=v;
a1[v]=i;
return 1;
}
}
}
return 0;
}


int maxmatch()
{
int ret=0;
memset(a1,-1,sizeof(a1));
memset(a2,-1,sizeof(a2));
for(int i=1;i<=n;i++)
{
if(a1[i]==-1)
{
memset(fl,0,sizeof(fl));
if(search(i))
{
ret++;
}
}
}
return ret;
}


int main()
{
while(~scanf("%d",&m))
{
memset(a,0,sizeof(a));
memset(g,0,sizeof(g));
map<string,int>mp;
int cnt=0;
for(int i=1;i<=m;i++)
{
cin>>plug[i];
int x=mp[plug[i]];
if(x==0)
{
cnt++;
x=cnt;
mp[plug[i]]=cnt;
g[x][x]=1;
}
}


scanf("%d",&n);
for(int i=1;i<=n;i++)
{
string s;
cin>>s>>dev[i];
int x=mp[dev[i]];
if(x==0)
{
cnt++;
x=cnt;
mp[dev[i]]=cnt;
g[x][x]=1;
}
}


scanf("%d",&k);
for(int i=0;i<k;i++)
{
string s1,s2;
cin>>s1>>s2;
int x=mp[s1];
int y=mp[s2];
if(x==0)
{
cnt++;
x=cnt;
mp[s1]=cnt;
g[x][x]=1;
}
if(y==0)
{
cnt++;
y=cnt;
mp[s2]=cnt;
g[y][y]=1;
}
g[x][y]=1;
}
floyd(cnt);//用floyd算法解决转换器连转换器的问题
//建图,分别取设备需要的插座的编号和存在的插座的编号,如果两个可以相连,那么设备i到插座j有边
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(g[mp[dev[i]]][mp[plug[j]]]==1)
{
a[i][j]=1;
}
}
}
int res=maxmatch();
printf("%d\n",n-res);
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值