限制起终点的可相交最小路径覆盖。首先tarjan缩一波点。然后就是个有上下界最小流,拆点,i->i+scc,容量为(1,inf)。其他边都是(0,inf)。
tips:这题蒟蒻有个不是很懂的地方,我的超级汇点T=2001时就会wa,改成2005就可以A了,是我的代码哪里写挂了吗?望神犇指教。
或者可以用最大费用最大流,拆点,i->i+scc,容量为1,花费为1的一条边,再来一条容量为inf,花费为0的边,其他边都是容量inf,花费0.跑最大费用最大流,增广次数就是答案。因为最大费用网络流保证了正确性。(当最大费用增光路也为0时就要退出了)
#include
#include
#include
#include
#include
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 2010
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,m,h[N],num=1,lev[N],cur[N],A,B,a[1010],b[1010],s=2003,t=2002,T=2005;
int dfn[N],low[N],dfnum=0,scc=0,bel[N];
bool inq[N];
struct edge{
int fr,to,next,val;
}data[31000];
inline void add(int x,int y,int val){
data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].val=val;
data[++num].to=x;data[num].next=h[y];h[y]=num;data[num].val=0;
}
inline void add(int x,int y){
data[++num].to=y;data[num].fr=x;data[num].next=h[x];h[x]=num;++num;
}stack<int>qq;
inline void tarjan(int x){
dfn[x]=low[x]=++dfnum;qq.push(x);inq[x]=1;
for(int i=h[x];i;i=data[i].next){
int y=data[i].to;
if(!dfn[y]) tarjan(y),low[x]=min(low[x],low[y]);
else if(inq[y]) low[x]=min(low[x],dfn[y]);
}if(low[x]==dfn[x]){
++scc;while(1){
int y=qq.top();qq.pop();inq[y]=0;
bel[y]=scc;if(y==x) break;
}
}
}
inline bool bfs(){
queue<int>q;memset(lev,0,sizeof(lev));
q.push(0);lev[0]=1;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=h[x];i;i=data[i].next){
int y=data[i].to;if(lev[y]||!data[i].val) continue;
lev[y]=lev[x]+1;if(y==T) return 1;q.push(y);
}
}return 0;
}
inline int dinic(int x,int low){
if(x==T) return low;int tmp=low;
for(int &i=cur[x];i;i=data[i].next){
int y=data[i].to;if(lev[y]!=lev[x]+1||!data[i].val) continue;
int res=dinic(y,min(tmp,data[i].val));
if(!res) lev[y]=0;else tmp-=res,data[i].val-=res,data[i^1].val+=res;
if(!tmp) return low;
}return low-tmp;
}
int main(){
// freopen("a.in","r",stdin);
int tst=read();
while(tst--){
n=read();m=read();A=read();B=read();
memset(h,0,sizeof(h));num=1;int ans=0;
memset(dfn,0,sizeof(dfn));dfnum=0;scc=0;
for(int i=1;i<=A;++i) a[i]=read();
for(int i=1;i<=B;++i) b[i]=read();
while(m--){
int x=read(),y=read();add(x,y);
}for(int i=1;i<=n;++i) if(!dfn[i]) tarjan(i);
int num1=num;memset(h,0,sizeof(h));num=1;
for(int i=2;i<=num1;i+=2){
int x=bel[data[i].fr],y=bel[data[i].to];
if(x!=y) add(x+scc,y,inf);
}memset(inq,0,sizeof(inq));
for(int i=1;i<=A;++i){
int x=bel[a[i]];if(inq[x]) continue;
inq[x]=1;add(s,x,inf);
}memset(inq,0,sizeof(inq));
for(int i=1;i<=B;++i){
int x=bel[b[i]];if(inq[x]) continue;
inq[x]=1;add(x+scc,t,inf);
}memset(inq,0,sizeof(inq));
for(int i=1;i<=scc;++i) add(i,i+scc,inf),add(0,i+scc,1),add(i,T,1);
while(bfs()){memcpy(cur,h,sizeof(cur));ans+=dinic(0,inf);}
add(t,s,inf);
while(bfs()){memcpy(cur,h,sizeof(cur));ans+=dinic(0,inf);}
if(ans!=scc) puts("no solution");
else printf("%d\n",data[num].val);
}return 0;
}
#include
#include
#include
#include
#include
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 2010
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,m,h[N],num=1,dis[N],path[N],A,B,a[1010],b[1010],T=2005;
int dfn[N],low[N],dfnum=0,scc=0,bel[N];
bool f[N],inq[N];
struct edge{
int fr,to,next,w,c;
}data[31000];
inline void add(int x,int y,int w,int c){
data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].w=w;data[num].c=c;
data[++num].to=x;data[num].next=h[y];h[y]=num;data[num].w=0;data[num].c=-c;
}
inline void add(int x,int y){
data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].fr=x;++num;
}stack<int>qq;
inline void tarjan(int x){
dfn[x]=low[x]=++dfnum;qq.push(x);inq[x]=1;
for(int i=h[x];i;i=data[i].next){
int y=data[i].to;
if(!dfn[y]) tarjan(y),low[x]=min(low[x],low[y]);
else if(inq[y]) low[x]=min(low[x],dfn[y]);
}if(low[x]!=dfn[x]) return;++scc;
while(1){
int y=qq.top();qq.pop();inq[y]=0;
bel[y]=scc;if(y==x) break;
}
}
inline bool spfa(){
deque<int>q;memset(path,0,sizeof(path));memset(dis,128,sizeof(dis));
q.push_back(0);inq[0]=1;dis[0]=0;
while(!q.empty()){
int x=q.front();q.pop_front();inq[x]=0;
for(int i=h[x];i;i=data[i].next){
int y=data[i].to;if(!data[i].w) continue;
if(dis[x]+data[i].c>dis[y]){
dis[y]=dis[x]+data[i].c;path[y]=i;
if(!inq[y]){
if(!q.empty()&&dis[y]>dis[q.front()]) q.push_front(y);
else q.push_back(y);inq[y]=1;
}
}
}
}return path[T];
}
int main(){
// freopen("a.in","r",stdin);
int tst=read();
while(tst--){
n=read();m=read();A=read();B=read();
memset(h,0,sizeof(h));num=1;int ans=0,cost=0;
memset(dfn,0,sizeof(dfn));dfnum=0;scc=0;
for(int i=1;i<=A;++i) a[i]=read();
for(int i=1;i<=B;++i) b[i]=read();
while(m--){
int x=read(),y=read();add(x,y);
}for(int i=1;i<=n;++i) if(!dfn[i]) tarjan(i);
int num1=num;memset(h,0,sizeof(h));num=1;
for(int i=2;i<=num1;i+=2){
int x=bel[data[i].fr],y=bel[data[i].to];
if(x!=y) add(x+scc,y,inf,0);
}memset(inq,0,sizeof(inq));
for(int i=1;i<=A;++i){
int x=bel[a[i]];if(inq[x]) continue;
inq[x]=1;add(0,x,inf,0);
}memset(inq,0,sizeof(inq));
for(int i=1;i<=B;++i){
int x=bel[b[i]];if(inq[x]) continue;
inq[x]=1;add(x+scc,T,inf,0);
}memset(inq,0,sizeof(inq));
for(int i=1;i<=scc;++i) add(i,i+scc,inf,0),add(i,i+scc,1,1);
while(spfa()){
int low=inf,now=T;if(!dis[T]) break;
while(path[now]) low=min(low,data[path[now]].w),now=data[path[now]^1].to;
cost+=dis[T]*low;now=T;ans++;
if(cost==scc) break;
while(path[now]) data[path[now]].w-=low,data[path[now]^1].w+=low,now=data[path[now]^1].to;
}if(cost!=scc) puts("no solution");
else printf("%d\n",ans);
}return 0;
}