HDU-3081-Marriage Match II

HDU-3081-Marriage Match II

http://acm.hdu.edu.cn/showproblem.php?pid=3081

女生和男生配对,有些女生相互是朋友,每个女生也可以跟她朋友所配对的男生配对

每次配对,每个女生都要跟不同的男生配对。问最多能配对几轮。

最大流,用并查集处理女生之间的朋友关系,最少配0轮,最多配n轮,二分解之,源点向女生建边,男生向汇点建边,容量均为mid,女生跟所有能配对的男生连线,容量为1如果最大流 = mid * n,那mid就就能做到mid轮配对

#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 500;
const int maxm = 50000;
const int INF=0x7fffffff;
int idx,n,m,p;
int cur[maxn], pre[maxn];
int dis[maxn], gap[maxn];
int aug[maxn], head[maxn];
int f[maxn];
int hashh[maxn][maxn];
struct Node
{
	int u, v, w;
	int next;
}edge[maxm];
struct peo
{
	int x;
	int y;
}peo[maxm];
void init()
{
	int i;
	for(i=1;i<=n;i++)
	f[i]=i;
}
int find(int x)
{
	int r=x;
	while(f[r]!=r)
	r=f[r];
	f[x]=r;
	return f[x];
}
void Union(int x,int y)
{
	int xx,yy;
	xx=find(x);
	yy=find(y);
	if(xx!=yy)
	f[xx]=yy;
}
void addEdge(int u, int v, int w)
{
    edge[idx].u = u;
    edge[idx].v = v;
    edge[idx].w = w;
    edge[idx].next = head[u];
    head[u] = idx++;
    edge[idx].u = v;
    edge[idx].v = u;
    edge[idx].w = 0;
    edge[idx].next = head[v];
    head[v] = idx++;
}
void build(int mid)
{
	int i,j,st,ed,a,b;
	idx = 0;
    memset(head, -1, sizeof(head));
	st=0;
	ed=2*n+1;
	for(i=1;i<=n;i++)
	{
		addEdge(st,i,mid);  //源点向女孩建边
		addEdge(i+n,ed,mid); //男孩向汇点建边
	}
	memset(hashh,0,sizeof(hashh));
	for(i=0;i 0 && dis[u] == dis[v] + 1) // 找到允许弧
            {
                flag = true;
                pre[v] = u;
                cur[u] = id;
                aug[v] = min(aug[u], edge[id].w);
                u = v;
                break;
            }
        }
        if (flag == false)
        {
            if (--gap[dis[u]] == 0) break; /* gap优化,层次树出现断层则结束算法 */
            mindis = n;
            cur[u] = head[u];
            for (id = head[u]; id != -1; id = edge[id].next)
            {
                v = edge[id].v;
                if (edge[id].w > 0 && dis[v] < mindis)
                {
                    mindis = dis[v];
                    cur[u] = id; // 修改标号的同时修改当前弧
                }
            }
            dis[u] = mindis + 1;
            gap[dis[u]]++;
            if (u != s) u = pre[u]; // 回溯继续寻找允许弧
        }
    }
    return max_flow;
}
int main()
{
    int i,t,ans;
	int a,b;
	int low,high,mid;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&p);
		for(i=0;i


你可能感兴趣的:(HDU-3081-Marriage Match II)