描述
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.
输入
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.
输出
A line containing a single non-negative integer indicating the smallest number of devices that cannot be plugged in.
样例输入
4
A
B
C
D
5
laptop B
phone C
pager B
clock B
comb X
3
B X
X A
X D
样例输出
1
题意
n个插头,m个插座,k个交换器,输出最少多少插头没有插入插座
题解
插头连源点S流量为字符重复的次数,插座连汇点T流量为字符出现的次数,连交换器边流量INF,跑最大流即可
这里用哈希建图比较方便,然后交换器边是后面的指向前面的
代码
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int maxn=1e5+5; 5 const int maxm=2e5+5; 6 int n,m,S,T; 7 int deep[maxn],q[400000]; 8 int FIR[maxn],TO[maxm],CAP[maxm],COST[maxm],NEXT[maxm],tote; 9 10 void add(int u,int v,int cap) 11 { 12 TO[tote]=v; 13 CAP[tote]=cap; 14 NEXT[tote]=FIR[u]; 15 FIR[u]=tote++; 16 17 TO[tote]=u; 18 CAP[tote]=0; 19 NEXT[tote]=FIR[v]; 20 FIR[v]=tote++; 21 } 22 bool bfs() 23 { 24 memset(deep,0,sizeof deep); 25 deep[S]=1;q[1]=S; 26 int head=0,tail=1; 27 while(head!=tail) 28 { 29 int u=q[++head]; 30 for(int v=FIR[u];v!=-1;v=NEXT[v]) 31 { 32 if(CAP[v]&&!deep[TO[v]]) 33 { 34 deep[TO[v]]=deep[u]+1; 35 q[++tail]=TO[v]; 36 } 37 } 38 } 39 return deep[T]; 40 } 41 int dfs(int u,int fl) 42 { 43 if(u==T)return fl; 44 int f=0; 45 for(int v=FIR[u];v!=-1&&fl;v=NEXT[v]) 46 { 47 if(CAP[v]&&deep[TO[v]]==deep[u]+1) 48 { 49 int Min=dfs(TO[v],min(fl,CAP[v])); 50 CAP[v]-=Min;CAP[v^1]+=Min; 51 fl-=Min;f+=Min; 52 } 53 } 54 if(!f)deep[u]=-2; 55 return f; 56 } 57 int maxflow() 58 { 59 int ans=0; 60 while(bfs()) 61 ans+=dfs(S,1<<28); 62 return ans; 63 } 64 void init() 65 { 66 tote=0; 67 memset(FIR,-1,sizeof FIR); 68 } 69 int N,M,K,tot=2; 70 map<string,int>has; 71 string s,s1; 72 int main() 73 { 74 init(); 75 S=0,T=1; 76 cin>>N; 77 for(int i=1;i<=N;i++) 78 { 79 cin>>s; 80 if(!has[s])has[s]=tot++; 81 add(S,has[s],1); 82 } 83 cin>>M; 84 for(int i=1;i<=M;i++) 85 { 86 cin>>s1>>s; 87 if(!has[s])has[s]=tot++; 88 add(has[s],T,1); 89 } 90 cin>>K; 91 for(int i=1;i<=K;i++) 92 { 93 cin>>s>>s1; 94 if(!has[s])has[s]=tot++; 95 if(!has[s1])has[s1]=tot++; 96 add(has[s1],has[s],1<<28); 97 } 98 printf("%d ",M-maxflow()); 99 return 0; 100 }