题意:与hdu3081题意大体相同,只不过增加了一个条件是:每个女孩可以额外选k个男生。
难度:3.5
题解:把每个女生拆分为两个点g,g',两点之间连接边,容量为k。g'链接那些还没有连接过的男生。二分最大配对次数。
难度:3.5
题解:把每个女生拆分为两个点g,g',两点之间连接边,容量为k。g'链接那些还没有连接过的男生。二分最大配对次数。
/*
这题跟Marriage Match II略有不同。
每个女孩除了跟自己喜欢的、朋友喜欢的男生配对以外,还可以额外跟k个其他不同的男生配对。
把每个女生拆分为两个点g,g',两点之间连接边,容量为k。
g'链接那些还没有连接过的男生。
二分最大配对次数。
*/
#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#define maxn 10000
#define maxm 205000
#define inf 2000000000
using namespace std;
int gap[maxn],dis[maxn],pre[maxn],cur[maxn];
int NV,m,n,f,k;
struct Edge {
int v,val;
int next;
Edge(){}
Edge( int V , int NEXT , int W = 0 ):v(V),next(NEXT),val(W){}
}edge[maxm];
int maxflow;
int cnt_edge,head[maxn];
void Insert_Edge( int u , int v , int flow = 0 ) {
//printf("%d->%d,%d\n",u,v,flow);
edge[cnt_edge] = Edge(v,head[u],flow);
head[u] = cnt_edge++;
edge[cnt_edge] = Edge(u,head[v]);
head[v] = cnt_edge++;
}
void Init() {
cnt_edge = 0;
memset(head,-1,sizeof(int)*(NV+1));
}
int Sap( int st, int en )
{
memset(dis,0,sizeof(int)*( NV+1));
memset(gap,0,sizeof(int)*( NV+1));
for( int i = 0 ; i < NV ; i++ )
cur[i] = head[i];
int u = pre[st] = st,maxflow = 0,aug = inf;
gap[0] = NV;
while( dis[st] < NV )
{
loop: for( int &i = cur[u]; i != -1 ; i = edge[i].next ) {
int v =edge[i].v;
if( edge[i].val && dis[u] == dis[v]+1) {
aug = aug < edge[i].val? aug: edge[i].val;
pre[v] = u;
u = v;
if( v == en ) {
maxflow += aug;
for( u = pre[u]; v != st ; v = u,u = pre[u] ) {
edge[cur[u]].val -= aug;
edge[cur[u]^1].val += aug;
}
aug = inf;
}
goto loop;
}
}
int mindis = NV;
for( int i = head[u]; i != -1 ; i = edge[i].next ) {
int v = edge[i].v;
if( edge[i].val && mindis > dis[v] ) {
cur[u] = i;
mindis = dis[v];
}
}
if( --gap[dis[u]] == 0 ) break;
gap[ dis[u] = mindis+1 ]++;
u = pre[u];
}
return maxflow;
}
pair<int,int> kp[1000005];
int root[255];
int GetRoot( int x ) {
int r = x;
while( r != root[r] ) r = root[r];
int i = root[x],j;
while( i != r ) {
j = root[i];
root[i] = r;
i = j;
}
return r;
}
bool Hash[255][255];
void Build_Graph( int mid ) {
NV = 3 * n + 2;
Init();
int st = 0,en = 3*n+1;
for( int i = 1 ; i <= n ; i++ ) {
Insert_Edge(st,i,mid);
Insert_Edge(i+n*2,en,mid);
Insert_Edge(i,i+n,k);
for( int j = 1 ; j <= n ; j++ )
Hash[i][j] = false;
}
for( int i = 0 ; i < m ; i++ ) {
int g = kp[i].first;
int b = kp[i].second;
for( int j = 1 ; j <= n ; j++ ) {
if( root[g] == root[j] && !Hash[j][b] ) {
Hash[j][b] = true;
Insert_Edge(j,b+2*n,1);
}
}
}
for( int i = 1 ; i <= n ; i++ )
for( int j = 1 ; j <= n ; j++ )
if( !Hash[i][j] )
Insert_Edge(i+n,j+2*n,1);
}
void Solve() {
int l = 0 , r = n;
int mid;
int ans = 0;
while( l <= r ) {
mid = ( l + r ) >> 1;
Build_Graph(mid);
if( Sap(0,3*n+1) == n * mid ) {
l = mid+1;
ans = mid;
} else r = mid-1;
}
printf("%d\n",ans);
}
int main() {
int t;
scanf("%d",&t);
while(t--) {
scanf("%d%d%d%d",&n,&m,&k,&f);
int a,b;
for( int i = 1 ; i <= n ; i++ )
root[i] = i;
for( int i = 0 ; i < m ; i++ )
scanf("%d%d",&kp[i].first,&kp[i].second);
while(f--) {
scanf("%d%d",&a,&b);
int fa = GetRoot(a);
int fb = GetRoot(b);
if( fa != fb ) {
root[fa] = fb;
}
}
for( int i = 1 ; i <= n ; i++ ) root[i] = GetRoot(i);
//for( int i = 1 ; i <= n ; i++ ) printf("root[%d] = %d\n",i,root[i]);
Solve();
}
return 0;
}