二分答案跑最大流。。

//author: CHC
//First Edit Time: 2014-08-11 16:00
//Last Edit Time: 2014-08-11 16:49
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits.h>
using namespace std;
typedef long long LL;
const int MAXN=1e+3;
const int MAXM=1e+5;
const int INF = INT_MAX;
//const int LL_INF = INT64_MAX;
struct Edge
{
int from,to,ci,next;
Edge(){}
Edge(int _from,int _to,int _ci,int _next):from(_from),to(_to),ci(_ci),next(_next){}
}e[MAXM];
int head[MAXN],tot;
int dis[MAXN];
int top,sta[MAXN],cur[MAXN];
inline void init(){
memset(head,-1,sizeof(head));
tot=0;
}
inline void AddEdge(int u,int v,int ci0,int ci1=0){
e[tot]=Edge(u,v,ci0,head[u]);
head[u]=tot++;
e[tot]=Edge(v,u,ci1,head[v]);
head[v]=tot++;
}
inline bool bfs(int st,int et){
memset(dis,0,sizeof(dis));
dis[st]=1;
queue <int> q;
q.push(st);
while(!q.empty()){
int now=q.front();
q.pop();
for(int i=head[now];i!=-1;i=e[i].next){
int next=e[i].to;
if(e[i].ci&&!dis[next]){
dis[next]=dis[now]+1;
if(next==et)return true;
q.push(next);
}
}
}
return false;
}
LL Dinic(int st,int et){
LL ans=0;
while(bfs(st,et)){
//printf("here\n");
top=0;
memcpy(cur,head,sizeof(head));
int u=st,i;
while(1){
if(u==et){
int pos,minn=INF;
//printf("top:%d\n",top);
for(i=0;i<top;i++)
{
if(minn>e[sta[i]].ci){
minn=e[sta[i]].ci;
pos=i;
}
//printf("%d --> %d\n",e[sta[i]].from,e[sta[i]].to);
}
for(i=0;i<top;i++){
e[sta[i]].ci-=minn;
e[sta[i]^1].ci+=minn;
}
top=pos;
u=e[sta[top]].from;
ans+=minn;
//printf("minn:%d\n\n",minn);
}
for(i=cur[u];i!=-1;cur[u]=i=e[i].next)
if(e[i].ci&&dis[u]+1==dis[e[i].to])break;
if(cur[u]!=-1){
sta[top++]=cur[u];
u=e[cur[u]].to;
}
else {
if(top==0)break;
dis[u]=0;
u=e[sta[--top]].from;
}
}
}
return ans;
}
int path[MAXN],cnt[MAXN];
int Find(int x){
return x==path[x]?x:path[x]=Find(path[x]);
}
bool Union(int x,int y){
x=Find(x);y=Find(y);
if(x==y)return true;
path[y]=x;
cnt[x]+=cnt[y];
cnt[y]=0;
return true;
}
set <int> ss[300];
set <int> ::iterator it;
int jihe[MAXN],prehead[MAXN];
int main()
{
int t;
int n,m,k,f;
scanf("%d",&t);
while(t--){
scanf("%d%d%d%d",&n,&m,&k,&f);
for(int i=0;i<MAXN;i++)
path[i]=i,cnt[i]=1;
init();
for(int i=0;i<300;i++)
ss[i].clear();
//1~250 girl
//251~500 boy
for(int i=0,x,y;i<m;i++){
scanf("%d%d",&x,&y);
ss[x].insert(y+250);
}
for(int i=0,x,y;i<f;i++){
scanf("%d%d",&x,&y);
Union(x,y);
}
jihe[0]=0;
for(int i=1;i<=n;i++)
if(path[i]==i){
jihe[++jihe[0]]=i;
}
else ss[Find(i)].insert(ss[i].begin(),ss[i].end());
int ans=1,flag=0;
int l=0,r=n;
//for(;ans<=n;ans++){
while(l<=r){
//0 -->源点
//501 -->汇点
ans=(l+r)>>1;
init();
int to1=502;
for(int i=1;i<=jihe[0];i++){
for(it=ss[jihe[i]].begin();it!=ss[jihe[i]].end();it++){
AddEdge(jihe[i],*it,cnt[jihe[i]]);
//printf("%d-->%d val:%d\n",jihe[i],*it,cnt[jihe[i]]);
}
AddEdge(jihe[i],to1,k*cnt[jihe[i]]);
//printf("%d-->%d val:%d\n",jihe[i],to1,k);
for(int j=251;j<=250+n;j++){
it=ss[jihe[i]].find(j);
if(it==ss[jihe[i]].end()){
AddEdge(to1,j,cnt[jihe[i]]);
//printf("%d-->%d val:%d\n",to1,j,cnt[jihe[i]]);
}
}
++to1;
}
for(int i=1;i<=jihe[0];i++)
{
AddEdge(0,jihe[i],ans*cnt[jihe[i]]);
//printf("%d-->%d val:%d\n",0,jihe[i],ans*cnt[jihe[i]]);
}
for(int i=251;i<=250+n;i++)
{
AddEdge(i,501,ans);
//printf("%d-->%d val:%d\n",i,201,ans);
}
//printf("ans:%d\n",ans);
LL tt=Dinic(0,501);
//printf("%I64d %I64d\n",tt,(LL)ans*n);
if(tt==(LL)ans*n){
flag=ans;
l=ans+1;
}
else r=ans-1;
}
printf("%d\n",flag);
}
return 0;
}