思路: 建图: 1. 插头向插排建图 2. 插头向转换器建图 3. 转换器向转换器建图 4. 转换器向插排建图 5. 建立原点,源点向插头建图 6. 建立汇点 ,插排向汇点建图。 跑一个网络流。
代码:
#include
using namespace std;
const int N=1e3+5;
const int inf =0x3f3f3f3f;
int totc;
struct node1
{
string s;
int num;
}s1[105];
struct node2
{
string s;
int num;
}s2[105];
struct node
{
string a;
string b;
int num;
}s3[105];
struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Dinic
{
int n,m,s,t;
vectoredges;
vectorG[N];
bool vis[N];
int d[N];
int cur[N];
void addedge(int from,int to,int cap)
{
edges.push_back(Edge(from,to,cap,0));
edges.push_back(Edge(to,from,0,0));
int m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool bfs()
{
memset(vis,0,sizeof(vis));
queueq;
q.push(s); d[s]=0; vis[s]=1;
while(!q.empty()){
int x=q.front(); q.pop();
for(int i=0;ie.flow)
{
vis[e.to]=1;
d[e.to]=d[x]+1;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t||a==0) return a;
int flow=0,f;
for(int &i=cur[x];i0){
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(!a) break;
}
}
return flow;
}
int maxflow(int s,int t)
{
this->s=s; this->t=t;
int flow=0;
while(bfs()){
memset(cur,0,sizeof(cur));
flow+=dfs(s,inf);
}
return flow;
}
}a;
int n,m,k;
int main()
{
int T;
scanf("%d",&T);
int ff=0;
while(T--)
{
//int in1,in2;
scanf("%d",&m);
for(int i=1;i<=m;i++){
cin>>s2[i].s;
s2[i].num=++totc;
}
//in1=totc+1;
scanf("%d",&n);
for(int i=1;i<=n;i++){
cin>>s1[i].s>>s1[i].s;
s1[i].num=++totc;
}
//in2 =totc+1;
scanf("%d",&k);
for(int i=1;i<=k;i++){
cin>>s3[i].a>>s3[i].b;
s3[i].num=++totc;
}
a.n=totc;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(s1[i].s==s2[j].s){
a.addedge(s1[i].num,s2[j].num,1);
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=k;j++){
if(s1[i].s==s3[j].a){
a.addedge(s1[i].num,s3[j].num,1);
}
}
}
for(int i=1;i<=k;i++)
{
for(int j=1;j<=k;j++)
{
if(i==j) continue;
if(s3[i].b==s3[j].a) a.addedge(s3[i].num,s3[j].num,1);
}
}
for(int i=1;i<=k;i++){
for(int j=1;j<=m;j++)
{
if(s3[i].b==s2[j].s){
a.addedge(s3[i].num,s2[j].num,1);
}
}
}
int st=++totc;
int en=++totc;
for(int i=1;i<=n;i++){
a.addedge(st,s1[i].num,1);
}
for(int i=1;i<=m;i++){
a.addedge(s2[i].num,en,1);
}
int ans=a.maxflow(st,en);
if(ff) cout<