#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<stack>
#include<set>
#include<bitset>
using namespace std;
const int MAX=205;
const int INF=0x3f3f3f3f;
int Dis[MAX][MAX],Layer[MAX],SSS,TTT;
bool Visited[MAX];
int F[MAX];
bool CountLayer()
{
deque<int> que;
memset(Layer,-1,sizeof(Layer));
Layer[SSS]=0;
que.push_back(SSS);
while (!que.empty())
{
int u=que.front();
que.pop_front();
for (int i=1; i<=TTT; i++)
if (Dis[u][i]>0&&Layer[i]==-1)
{
Layer[i]=Layer[u]+1;
if (i==TTT)
return true;
else
que.push_back(i);
}
}
return false;
}
int Dinic()
{
int Ans=0;
deque<int> que;
while (CountLayer())
{
que.push_back(SSS);
memset(Visited,false,sizeof(Visited));
Visited[SSS]=true;
while (!que.empty())
{
int u=que.back();
if (u==TTT)
{
int MinFlow=999999999,MinFlowStart;
for (int i=1; i<(int)que.size(); i++)
{
int S=que[i-1],E=que[i];
if (Dis[S][E]>0)
if (MinFlow>Dis[S][E])
MinFlow=Dis[S][E],MinFlowStart=S;
}
Ans+=MinFlow;
for (int i=1; i<(int)que.size(); i++)
{
int S=que[i-1],E=que[i];
Dis[S][E]-=MinFlow,Dis[E][S]+=MinFlow;
}
while (!que.empty()&&que.back()!=MinFlowStart)
{
Visited[que.back()]=false;
que.pop_back();
}
}
else
{
int i;
for (i=1; i<=TTT; i++)
if (Dis[u][i]>0&&Layer[i]==Layer[u]+1&&!Visited[i])
{
Visited[i]=true;
que.push_back(i);
break;
}
if (i>TTT)
que.pop_back();
}
}
}
return Ans;
}
inline void AddEdge(int a,int b,int c)
{
Dis[a][b]=c;
}
int main()
{
int T,N,M;
scanf("%d",&T);
while (T--)
{
memset(Dis,0,sizeof(Dis));
scanf("%d%d",&N,&M);
TTT=204,SSS=0;
for (int i=1;i<=M;i++)
{
cin>>F[i+101];
AddEdge(i+101,TTT,F[i+101]);
}
int a,b,c;
for (int i=1;i<=N;i++)
{
cin>>a>>b;
AddEdge(SSS,i,a);
for (int j=0;j<b;j++)
{
cin>>c;
AddEdge(i,c+101,1);
}
}
Dinic();
bool Ans=true;
for (int i=1;i<=M;i++)
if (Dis[i+101][TTT]!=0)
Ans=false;
if (Ans)
cout<<"Yes\n";
else
cout<<"No\n";
}
return 0;
}
HihoCoder 1393 二分图多重匹配 Dinic
最新推荐文章于 2022-03-12 09:26:12 发布