链接:

https://vjudge.net/problem/HDU-2444#author=634579757

题意:

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 that A and C know each other.

Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.

Calculate the maximum number of pairs that can be arranged into these double rooms.

有n个关系,他们之间某些人相互认识。这样的人有m对。

你需要把人分成2组,使得每组人内部之间是相互不认识的。

如果可以,就可以安排他们住宿了。安排住宿时,住在一个房间的两个人应该相互认识。

最多的能有多少个房间住宿的两个相互认识。

思路:

先二分图判定,再二分图最大匹配

代码:

#include <bits/stdc++.h>
using namespace std; const int MAXN = 2e2+10;
vector<int> G[MAXN];
int Linked[MAXN], Vis[MAXN];
int Color[MAXN];
int n, m; bool Dfs(int x)
{
for (int i = 0;i < G[x].size();i++)
{
int nextnode = G[x][i];
if (Vis[nextnode])
continue;
Vis[nextnode] = 1;
if (Linked[nextnode] == -1 || Dfs(Linked[nextnode]))
{
Linked[nextnode] = x;
return true;
}
}
return false;
} int Solve()
{
int cnt = 0;
memset(Linked, -1, sizeof(Linked));
for (int i = 1;i <= n;i++)
{
memset(Vis, 0, sizeof(Vis));
if (Dfs(i))
cnt++;
}
return cnt;
} bool Dfsb(int u, int v)
{
Color[u] = v;
for (int i = 0;i < G[u].size();i++)
{
if (Color[G[u][i]] == v)
return false;
if (Color[G[u][i]] == 0 && !Dfsb(G[u][i], -v))
return false;
}
return true;
} bool Binary()
{
memset(Color, 0, sizeof(Color));
for (int i = 1;i <= n;i++)
{
if (Color[i] == 0)
{
if (!Dfsb(i, 1))
return false;
}
}
return true;
} int main()
{
while (cin >> n >> m)
{
for (int i = 1;i <= n;i++)
G[i].clear();
int l, r;
for (int i = 1;i <= m;i++)
{
cin >> l >> r;
G[l].push_back(r);
}
if (!Binary())
{
cout << "No" << endl;
continue;
}
int cnt = Solve();
cout << cnt << endl;
} return 0;
}

HDU-2444-The Accomodation of Students(二分图判定,最大匹配)的更多相关文章

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

    题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...

  2. HDU 2444 The Accomodation of Students二分图判定和匈牙利算法

    本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配. 究竟怎样学习一种新算法呢? 我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己 ...

  3. HDU 2444 The Accomodation of Students (二分图存在的判定以及最大匹配数)

    There are a group of students. Some of them may know each other, while others don't. For example, A ...

  4. HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...

  5. HDU 2444 The Accomodation of Students (偶图判定,匈牙利算法)

    题意: 有一堆的学生关系,要将他们先分成两个组,同组的人都不互不认识,如果不能分2组,输出No.若能,则继续.在两组中挑两个认识的人(每组各1人)到一个双人房.输出需要多少个双人房? 思路: 先判定是 ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Me ...

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

    这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...

  8. hdu 2444 The Accomodation of Students 判断二分图+二分匹配

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

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

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

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

    The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

随机推荐

  1. freetype HarfBuzz fontconfig Cairo 编译顺序

    There is also a circular dependency between freetype and HarfBuzz. Note that fontconfig and Cario ar ...

  2. python学习之不要在列表迭代的时候进行增删操作

    注意:列表不能在for循环时使用remove方法 li = [11,22,33,44] for i in li : li.remove(i) print (li) #输出 [22, 44] ​ for ...

  3. gitlab ssh 免密登录

    打开本地git   使用 ssh-keygen 工具 输入命令  ssh-keygen -t rsa -C '你的邮箱账号' 接下来输入密码 确认密码 找到目录 找到公钥 在 gitlab 用户设置  ...

  4. Nginx日志监控工具

    ngxtop是一个基于python的程序,可以在Python上安装.ngxtop通过实时解析nginx访问日志, 并将结果(nginx服务器的有用指标)输出到终端. 主要的功能 当前有效请求 总请求计 ...

  5. 【深度学习笔记】第 2 课:Logistic 多项式回归法

    """Softmax.""" scores = [3.0, 1.0, 0.2] import numpy as np def softmax ...

  6. 最大公约数与最小公倍数C++实现

    最大公约数为辗转相除法求得, 最小公倍数为两数之积与最大公约数的比值 #include<iostream> using namespace std; int gcd(int, int); ...

  7. [转帖]/proc/sys/net/ipv4/ 下参数理解

    /proc/sys/net/ipv4/ 下参数理解,方便服务器优化 2017年06月02日 16:52:27 庞叶蒙 阅读数 3065 https://blog.csdn.net/pangyemeng ...

  8. 小记---------linux远程连接集群内其他机器mysql库

    mysql -h -u maxwell -p#10.0.15.145 远程机器ip#-P 注意是大写P 端口#-u 用户#-p 密码

  9. gcc 数据对齐之:总结篇.

    通过上面的分析,总结结构体对齐规则如下: 1.数据成员对齐规则:结构(struct)(或联合(union))的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员的对齐按照#pragm ...

  10. centos7 下网卡的配置

    一般通过修改配置文件的方式去修改: 网卡配置文件位置    /etc/sysconfig/network-scripts/ifcfg-ens33 DNS配置文件位置              /etc ...