B - The Accomodation of Students - hdu 2444(最大匹配)

题意:现在有一些学生给你一下朋友关系(不遵守朋友的朋友也是朋友),先确认能不能把这些人分成两组(组内的人要相互不认识),不能分的话输出No(小写的‘o’ - -,写成了大写的WA一次),能分的话,在求出来一对朋友可以住一个房间,最多可以开多少双人房(- -)

分析:使用并查集分组,如果出现矛盾就是不能分,可以分的话再用一组的成员求出最大匹配就OK了,还是比较水的....

*******************************************************************

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int MAXN = 205;

///rl记录与根节点的关系
int fa[MAXN], rl[MAXN];
int FindFather(int x)
{
    int k = fa[x];
    if(fa[x] != x)
    {
        fa[x] = FindFather(fa[x]);
        rl[x] = (rl[x]+rl[k]) % 2;
    }

return fa[x];
}

int x[MAXN], y[MAXN], nx, N;
bool G[MAXN][MAXN], used[MAXN];

void InIt()
{
    nx = 0;
    memset(G, 0, sizeof(G));
    for(int i=0; i<MAXN; i++)
    {
        fa[i] = i;
        x[i] = y[i] = 0;
        rl[i] = 0;
    }
}
bool FindOk(int u)
{
    for(int i=1; i<=N; i++)
    {
        if(G[u][i] && used[i] == false)
        {
            used[i] = true;
            if(!y[i] || FindOk(y[i]))
            {
                y[i] = u;
                return true;
            }
        }
    }

return false;
}

int main()
{
    int M;

while(scanf("%d%d", &N, &M) != EOF)
    {
        int i, u, v, ok=0;

InIt();

while(M--)
        {
            scanf("%d%d", &u, &v);
            G[u][v] = G[v][u] = true;

int ru = FindFather(u);
            int rv = FindFather(v);

if(ru == rv && rl[u] == rl[v])
                ok = 1;
            else if(ru != rv)
            {
                fa[ru] = rv;
                rl[ru] = (rl[u]+rl[v]+1)%2;
            }
        }

if(ok)
            printf("No\n");
        else
        {
            for(i=1; i<=N; i++)
            {
                u = FindFather(i);
                if(rl[i] == 0)
                    x[nx++] = i;
            }

int ans = 0;
            for(i=0; i<nx; i++)
            {
                memset(used, false, sizeof(used));
                if(FindOk(x[i]) == true)
                    ans++;
            }

printf("%d\n", ans);
        }
    }

return 0;

}

时间: 2024-11-10 15:57:42

B - The Accomodation of Students - hdu 2444(最大匹配)的相关文章

HDU 2444 The Accomodation of Students 二分图判定+最大匹配

题目来源:HDU 2444 The Accomodation of Students 题意:n个人是否可以分成2组 每组的人不能相互认识 就是二分图判定 可以分成2组 每组选一个2个人认识可以去一个双人间 最多可以有几组 思路:二分图判定+最大匹配 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn = 550; int vis[maxn];

(hdu)2444 The Accomodation of Students 判断二分图+最大匹配数

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply t

HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)

[题目链接]:click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则输出两组间俩人互相认识的对数 [解题思路]:   先推断是否能构成二分图,推断二分图用交叉染色法:从某个未染色的点出发把此点染成白色,该点周围的点染成黑色.黑色周围的又染成白色.若走到某个点已经染色,而且它相邻点的颜色与它一样则不是二分图,能够这样理解,染白色既增加X集合,黑色既增加Y集合,若某个点即是X集合又是Y集合,那说明不是二分

Hdu_2444 The Accomodation of Students -二分图判断+最大匹配

题意:先判断学生能否分成两块互不认识的集体,然后找出这两块集体的最大匹配 吐槽:迷之WA了几次,后来把判断是否是二分图弄成函数写在外面就ok了. /************************************************ Author :DarkTong Created Time :2016/8/1 13:02:46 File Name :Hdu_2444.cpp *************************************************/ //#

hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2444 Description There are a group of students. Some of them may know

HDU 2444 The Accomodation of Students (判断是否是二分图,然后求最大匹配)

The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Description There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B

hdu 2444 The Accomodation of Students 判断是否为二分图+最大匹配

The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3042    Accepted Submission(s): 1425 Problem Description There are a group of students. Some of them may know each o

hdu 2444 The Accomodation of Students (判断二分图,最大匹配)

The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8939    Accepted Submission(s): 3925 Problem DescriptionThere are a group of students. Some of them may know each othe

hdu 2444 The Accomodation of Students

The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2458    Accepted Submission(s): 1177 Problem Description There are a group of students. Some of them may know each ot