hdu2444The Accomodation of Students

思路:

二分图判断+最大匹配模板

二分图判断的方法很好想,没有离散的基础凭空给你个图让你判断也很容易想到染色法,简单的介绍下就是用queue来做,标记一个点为x则他所有的邻点都为x‘,然后递归的执行下去。

接下来会面临一个比较有趣的问题,我们确定现在的图是二分图,然后我们要求它的最大匹配——这里涉及到一个很关键的问题,就是一个图我们说他自己是一个二分图,那么是他内部的一些点会分成两部分,分别写成两列变成了形式上的二分图。而我们用find求二分图的时候是分别写成两列的话是一个图的所有点,因此总数最后是要除以2的。

AC代码:

#include <iostream>
#include <cstring>
#include <queue>
#define maxn 207
#define INF 9999999
using namespace std;

int n,m;
int G[maxn][maxn];
int mark[maxn];
int vis[maxn];
int match[maxn];
int x,y;

bool find(int x)
{
    for(int i = 1;i <= n;i++)
        if(!vis[i] && G[x][i])
        {
            vis[i] = 1;
            if(match[i]==-1 || find(match[i]))
            {
                match[i] = x;
                return true;
            }
        }
    return false;
}

int main()
{
    while(cin>>n>>m)
    {
        int flag = 1;
        queue<int> qv;
        memset(G,0,sizeof(G));
        memset(vis,0,sizeof(vis));
        memset(mark,-1,sizeof(mark));
        int t1,t2;
        for(int i = 1;i <= m;i++)
        {
            cin>>t1>>t2;
            G[t1][t2] = G[t2][t1] = 1;
        }
        mark[1] = 0;
        qv.push(1);
        while(!qv.empty())
        {
            int col = qv.front();
            qv.pop();
            if(vis[col]) continue;
            vis[col] = 1;
            for(int i = 1;i <= n;i++)
                if(G[col][i])
                {
                    if(mark[i] == -1) {
                        mark[i] = mark[col]==0?1:0;
                        qv.push(i);
                    }
                    else if(mark[i] != mark[col])
                        continue;
                    else {
                        flag = 0;
                        break;
                    }
                }
            if(flag == 0) break;
        }
        if(!flag) {
            cout<<"No"<<endl;
            continue;
        }
        int ans = 0;
        memset(match,-1,sizeof(match));
        for(int i = 1;i <= n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(find(i)) ans++;
        }
        cout<<ans/2<<endl;
    }
    return 0;
}
时间: 2024-10-14 00:05:26

hdu2444The Accomodation of Students的相关文章

hdu2444The Accomodation of Students【判断二分图+最大匹配】

我觉得有必要粘一下英文: The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2502    Accepted Submission(s): 1190 Problem Description There are a group of students. Some of them may

hdu2444The Accomodation of Students (最大匹配+判断是否为二分图)

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

hdu2444 The Accomodation of Students(判断二分匹配+最大匹配)

//判断是否为二分图:在无向图G中,如果存在奇数回路,则不是二分图.否则是二分图. //判断回路奇偶性:把相邻两点染成黑白两色,如果相邻两点出现颜色相同则存在奇数回路.也就是非二分图. # include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int vis[210],map[210][210],cott[210]; int c[210]; int flag,n

HD2444The Accomodation of Students(并查集判断二分图+匹配)

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

【HDOJ】2444 The Accomodation of Students

图论的题目.着色原理+二分图匹配. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 205 5 6 char map[MAXN][MAXN]; 7 int link[MAXN]; 8 int color[MAXN]; 9 bool visit[MAXN]; 10 int n, m; 11 12 bool dfs(int v, int col) { 13 int i; 14 15 for (i=1; i<=n;

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

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 (判断是否是二分图,然后求最大匹配)

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(判断是否是二分图)

题目链接 题意:n个学生,m对关系,每一对互相认识的能住一个房间.问否把这些学生分成两组,要求每组的学生都互不认识.求最多需要多少个房间. 是否能分成两组?也就是说判断是不是二分图,判断二分图的办法,用染色法 把初始点染成黑色,然后与之相连的染成白色,重复,使路径黑白相间, 如果当前点的颜色和与他相连点的颜色相同时,则说明这个图不是二分图 求最多需要多少个房间?也就是求最大匹配数. #include <iostream> #include <cstdio> #include <