ZJU 1157 A Plug for UNIX

Just_ACM 2010-04-18 11:16:59
A Plug for UNIX
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.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


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

1

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

先用DFS,搜索出插头这件的对应关系,然后建立邻接矩阵,然后就是进行最大匹配了。。。。解题思路比较清楚,只是还是悲剧了。。。。


代码:

#include <iostream>
#include <string>
#include <cstring>
#define MAX 500
using namespace std;

int pnum;//插头数
int dnum; //设备数
int anum; //适配器数
string plug[MAX]; //存储插头型号
string type[MAX]; //设备插头型号
string dev[MAX]; //设备名字
string inadapt[MAX]; //需要的型号
string outadapt[MAX]; //适配的型号
bool map[MAX][MAX]; //二分矩阵
bool state[MAX];
bool flag[MAX]; //匹配插头时的标记
int res[MAX]; //匹配结果
void DFS(string type, int j, int d)
{
int i,k;
string temp;
for(i = 0; i< anum; i++)
if(type == outadapt[i] && flag[j] == true)
{
flag[i] = false;
DFS(inadapt[i], i,d);
}
temp= inadapt[j];
for(k = 0; k< pnum; k++)
{
if(temp == plug[k])
map[d][k] = true;
}
}

bool find(int i)
{
int j;
for(j = 0; j< dnum; j++)
if(map[i][j] == true && state[j] == false)
{
state[j] = true;
if(res[j] == -1 || find(res[j]) == true)
{
res[j] = i;
return true;
}
}
return false;
}


int main()
{
int n,p1=1;
cin>>n;
while(n--)
{
if(p1)
p1=0;
else
printf("\n");
int i,j,match;
string temp;
memset(map, false, sizeof(map));
for(i=0 ;i< MAX; i++)
res[i] = -1;

cin>>pnum;
for(i=0; i<pnum; i++) //输入插头型号
cin>>plug[i];
cin>>dnum;
for(i=0; i<dnum; i++) //输入设备名字和设备所需的插头型号
cin>>dev[i]>>type[i];
cin>>anum;
for(i=0; i<anum; i++) //输入适配器的适配型号,和所需的型号
cin>>outadapt[i]>>inadapt[i];

///////////////////////////////////////////////////////////////////////////////////////
for(i = 0; i<dnum; i++)
for(j = 0; j< anum; j++)
if(type[i] == outadapt[j])
{
memset(flag, true, sizeof(flag));
DFS(inadapt[j],j,i);
}

for(i=0; i<dnum; i++)
for(j=0; j< pnum; j++)
if(type[i] == plug[j])
map[i][j] = true;

match = 0;
/////////////////////////////////////// 匈牙利算法
for(i = 1; i<=pnum; i++)
{
memset(state, false , sizeof(state));
if(find(i))
match++;
}
cout<<dnum-match<<endl;

}
return 0;
}


通过了所有测试数据,就是不能ac, 希望大神们,帮忙看看是什么原因,如果能找到一个反例数据,本人万分感谢。
...全文
76 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Just_ACM 2010-04-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 logiciel 的回复:]
以下是一个反例:

1

2
A
B
2
laptop A
phone B
2
B A
A B
[/Quote]

非常感谢logiciel给我的反例数据,我再次检查代码的时候,发现了很多问题,哎!(粗心的毛病就是无法克服。)但归根揭底还是对DFS的不熟练,在匈牙利算法中也出现了一点失误,(把邻接矩阵的行和列看反了,囧!!)

最后再把这道题目的思路说一遍吧:从适配器开始下手,根据设备需要的插头型号,在适配器中找到可以提供这个型号插头的适配器,如果该适配器所需要的插头型号在原有的插头型号中可以找到,就把该设备与原有的插头关联(在邻接矩阵中置1.)接着,再把该适配器所需要的型号的插头作为参数DFS,(其中要注意flag的设置,以免出现死循环。)总的思路就是把与该设备有关联的原先的插头型号都标记出来,搜索过适配后,不要忘了原有的插头型号,也要做上相应标记。最后一步,就是二分匹配,然后把总的设备数减去匹配数就行了。
更正后代码如下:
#include <iostream>
#include <string>
#include <cstring>
#define MAX 500
using namespace std;

int pnum;//插头数
int dnum; //设备数
int anum; //适配器数
string plug[MAX]; //存储插头型号
string type[MAX]; //设备插头型号
string dev[MAX]; //设备名字
string inadapt[MAX]; //需要的型号
string outadapt[MAX]; //适配的型号
bool map[MAX][MAX]; //二分矩阵
bool state[MAX];
bool flag[MAX]; //匹配插头时的标记
int res[MAX]; //匹配结果
void DFS(string type, int j, int d)
{
int i,k;
string temp;
for(i = 0; i< anum; i++)
if(type == outadapt[i] && flag[i] == true)
{
flag[i] = false;
temp= inadapt[i];
for(k = 0; k< pnum; k++)
{
if(temp == plug[k])
map[d][k] = true;
}
DFS(inadapt[i], i,d);
}

}

bool find(int i)
{
int j;
for(j = 0; j< pnum; j++)
if(map[i][j] == true && state[j] == false)
{
state[j] = true;
if(res[j] == -1 || find(res[j]) == true)
{
res[j] = i;
return true;
}
}
return false;
}


int main()
{
int n,p1=1;
cin>>n;
while(n--)
{
if(p1)
p1=0;
else
printf("\n");
int i,j,match;
string temp;
memset(map, false, sizeof(map));
for(i=0 ;i< MAX; i++)
res[i] = -1;

cin>>pnum;
for(i=0; i<pnum; i++) //输入插头型号
cin>>plug[i];
cin>>dnum;
for(i=0; i<dnum; i++) //输入设备名字和设备所需的插头型号
cin>>dev[i]>>type[i];
cin>>anum;
for(i=0; i<anum; i++) //输入适配器的适配型号,和所需的型号
cin>>outadapt[i]>>inadapt[i];

///////////////////////////////////////////////////////////////////////////////////////
for(i = 0; i<dnum; i++)
for(j = 0; j< anum; j++)
if(type[i] == outadapt[j])
{
memset(flag, true, sizeof(flag));
DFS(outadapt[j],j,i);
}

for(i=0; i<dnum; i++)
for(j=0; j< pnum; j++)
if(type[i] == plug[j])
map[i][j] = true;

match = 0;
/////////////////////////////////////// 匈牙利算法
for(i = 0; i<dnum; i++)
{
memset(state, false , sizeof(state));
if(find(i))
match++;
}
cout<<dnum-match<<endl;
}
return 0;
}

soswaidao 2010-04-21
  • 打赏
  • 举报
回复
友情帮顶,友情帮顶
logiciel 2010-04-21
  • 打赏
  • 举报
回复
以下是一个反例:

1

2
A
B
2
laptop A
phone B
2
B A
A B
Just_ACM 2010-04-20
  • 打赏
  • 举报
回复
有没有大神来指点下啊?

64,687

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧