hdu1845 Jimmy’s Assignment --- 完整匹配

意甲冠军:

它需要一个特殊的图,以找到最大匹配。该图的特征是:无向图,度的每个节点3。这是一个双边连接组件(the graph is 2-edge-connected (that is, at least 2 edges need to be removed in order to make the graph disconnected) 这一点是这样理解的把。。)

思路:

一般想法就直接建图求最大匹配,点的范围是5000,不优化可能超时,以下代码是890ms过的。

还有一种思路:

完备匹配的条件:

1. G是K(K>0)次正则二分图

2.G是无桥的三次正则图

3.G在去掉随意一个顶点子集S后,其子图中含顶点数为奇数的连通分支数不大于|S|

具有以上三个特征的图一定有完备匹配。且当中第三点是完备匹配的充要条件。

据此可得。题目中所给的图一定是完备匹配。答案是n/2。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
const int maxn=5010;
using namespace std;

int main()
{
    int n,a,b,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<3*n/2;i++)
            scanf("%d%d",&a,&b);
        printf("%d\n",n/2);
    }
    return 0;
}
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
const int maxn=5010;
using namespace std;

int mx[maxn],my[maxn],n;
bool vis[maxn];
vector<int> e[maxn];

int path(int i)
{
    int j,sz=e[i].size();
    for(j=0;j<sz;j++)
    {
        int tmp=e[i][j];
        if(!vis[tmp])
        {
            vis[tmp]=1;
            if(my[tmp]==-1||path(my[tmp]))
            {
                my[tmp]=i;
                mx[i]=tmp;
                return 1;
            }
        }
    }
    return 0;
}

int hungary()
{
    int res=0;
    memset(mx,-1,(n+2)*sizeof(int));
    memset(my,-1,(n+2)*sizeof(int));
    for(int i=1;i<=n;i++)
    {
        if(mx[i]==-1)
        {
            memset(vis,0,(n+2)*sizeof(vis[0]));
            res+=path(i);
        }
    }
    return res;
}

int main()
{
    int T,m,a,b,i;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        m=3*n/2;
        for(i=1;i<=n;i++)
            e[i].clear();
        while(m--)
        {
            scanf("%d%d",&a,&b);
            e[a].push_back(b);
            e[b].push_back(a);
        }
        printf("%d\n",hungary()/2);
    }
    return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

时间: 2024-10-25 07:14:43

hdu1845 Jimmy’s Assignment --- 完整匹配的相关文章

hdu1845 Jimmy’s Assignment --- 完备匹配

题意: 要求在一个特殊的图上找最大匹配,该图特点是:无向图,每个节点度数为3,是一个边双连通分量(the graph is 2-edge-connected (that is, at least 2 edges need to be removed in order to make the graph disconnected) 这一点是这样理解的把..) 思路: 一般想法就直接建图求最大匹配,点的范围是5000,不优化可能超时,下面代码是890ms过的. 另一种思路: 完备匹配的条件: 1.

HDU1845 Jimmy’s Assignment(最大匹配)卡时间

Jimmy's Assignment Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1093    Accepted Submission(s): 446 Problem Description Jimmy is studying Advanced Graph Algorithms at his university. His mos

HDU - 1845 Jimmy’s Assignment (二分匹配)

Description Jimmy is studying Advanced Graph Algorithms at his university. His most recent assignment is to find a maximum matching in a special kind of graph. This graph is undirected, has N vertices and each vertex has degree 3. Furthermore, the gr

hdu 1845 Jimmy’s Assignment (二分图)

Jimmy's Assignment Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 896    Accepted Submission(s): 379 Problem Description Jimmy is studying Advanced Graph Algorithms at his university. His most

Makefile:(实验)多个目标匹配时会采用最完整匹配的目标

结论源自实验测试,如果有疏漏希望指出 当Makefile中存在多个匹配的目标时,Makefile会采用哪个匹配的目标呢? 测试的Makefile如下: .PHONY: all clean quick_sort all: quick_sort bubble_sort #目标1 quick_sort: @echo "matching [email protected]" @echo 'in quick_sort' #目标2 %: @echo "matching [email pr

Poj 3189 Steady Cow Assignment (多重匹配)

题目链接: Poj 3189 Steady Cow Assignment 题目描述: 有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度.当然啦,棚子也是有脾气的,并不是奶牛想住进来就住进来,超出棚子的最大容量了,棚子是拒绝的.现在要给每个奶牛安家,本宝宝是一个公正无私的人类,所以要找一个奶牛喜爱程度差值最小的方案(虽然这样大家的喜爱程度可能普遍偏低,因为绝对公平并不代表合理啊),问喜爱程度的区间最小为多大? 解题思路: 每个棚子并不是住一个奶牛,所以是多重匹配咯.匹配的时候二分枚举喜爱程度的

poj3189 Steady Cow Assignment --- 多重匹配,二分图匹配解法

有n头牛,m个牛棚,每头牛对牛棚的满意程度有一个排序,每个牛棚有牛数限制. 问如何分配各个牛,使得所有牛的满意程度的差值最小. 这题首先可以想到二分答案,对于每一种差值来求是否可行. 不想再搞网络流,学习了下二分图匈牙利解法.. 匹配时,对于每一种选择(牛棚),若满足范围,且有多余的容量,则匹配: 否则,对于该牛棚已经匹配过的牛进行增广. #include <iostream> #include <cstdlib> #include <cstring> #include

Hdu_1845 Jimmy&rsquo;s Assignment -最大匹配裸题

题意:裸题,看清楚边数不多这个条件就可以了. /************************************************ Author :DarkTong Created Time :2016/8/1 20:29:16 File Name :Hdu_1845.cpp *************************************************/ //#include <bits/stdc++.h> #include <cstdio> #

day6 反射,hashlib模块,正则匹配,冒泡,选择,插入排序

一.反射(自省) 首先通过一个例子来看一下本文中可能用到的对象和相关概念. import sys # 模块,sys指向这个模块对象import inspectdef foo(): pass # 函数,foo指向这个函数对象 class Cat(object): # 类,Cat指向这个类对象 def __init__(self, name='kitty'): self.name = name def sayHi(self): # 实例方法,sayHi指向这个方法对象,使用类或实例.sayHi访问