图论|判断一个无向图是不是二分图
判断一个无向图是不是二分图,使用染色法.对每个顶点的相邻顶点染与顶点不同的颜色。如果染过色且与顶点颜色相同,则不是二分图。
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 202;
vectormp[maxn];
int color[maxn];
bool bfs(int s)
{
color[s] = 0;
//连通分支的起点可以染1也可以染0
queueq;
q.push(s);
while (!q.empty())
{
s = q.front();
q.pop();
for (int i = 0;
i < (int)mp[s].size();
i ++) {
int t = mp[s][i];
if (color[t] == -1) color[t] = !color[s], q.push(t);
//如果相邻顶点未被染色,则加入
//队列。
if (color[t] == color[s]) { //如果相邻顶点染相同颜色,则不是二分图。
return false;
}
}
}
return true;
}
int main()
{
int n, m;
while (scanf("%d%d", &n, &m) != EOF)
{
for (int i = 1;
i <= n;
i ++) mp[i].clear();
memset(color, -1, (n+1)*sizeof(color[0]));
for (int i = 0;
i < m;
i ++) {
int x, y;
cin >> x>>y;
mp[x].push_back(y);
//无向图存储两条边
mp[y].push_back(x);
}
int flag = true;
//初始化无向图是二分图
for (int i = 1;
i <= n;
i ++) {
if (color[i] == -1 && !bfs(i)) { //对每个连通分支染色,如果两个相邻的点
//颜色相同,则不是二分图。
flag = false;
break;
}
}
if (!flag) cout <<"no"<
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2757Accepted Submission(s): 1287
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 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.
Input For each data set:
The first line gives two integers, n and m(1
Proceed to the end of file.
Output If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.
Sample Input
4 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 6
Sample Output
No 3
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 202;
vectormp[maxn];
int color[maxn];
int matchx[maxn];
int matchy[maxn];
bool vis[maxn];
bool bfs(int s)
{
color[s] = 0;
//连通分支的起点可以染1也可以染0
queueq;
q.push(s);
while (!q.empty())
{
s = q.front();
q.pop();
for (int i = 0;
i < (int)mp[s].size();
i ++) {
int t = mp[s][i];
if (color[t] == -1) color[t] = !color[s], q.push(t);
//如果相邻顶点未被染色,则加入
//队列。
if (color[t] == color[s]) { //如果相邻顶点染相同颜色,则不是二分图。
return false;
}
}
}
return true;
}bool dfs(const int& x, const int& n)
{
for (int i = 0;
i < (int)mp[x].size();
i ++) {
int j = mp[x][i];
if (vis[j] == false) {
vis[j] = true;
if (matchy[j] == -1 || dfs(matchy[j], n)) {
matchy[j] = x;
matchx[x] = j;
return true;
}
}
}
return false;
}int work(const int& n)
{
int res = 0;
memset(matchx, -1, (n+1)*sizeof(matchx[0]));
memset(matchy, -1, (n+1)*sizeof(matchy[0]));
for (int i = 1;
i <= n;
i ++) {
memset(vis, 0, (n+1)*sizeof(vis[0]));
if (matchx[i] == -1 && dfs(i, n)) res ++;
}
return res;
}int main()
{
int n, m;
while (scanf("%d%d", &n, &m) != EOF)
{
for (int i = 1;
i <= n;
i ++) mp[i].clear();
memset(color, -1, (n+1)*sizeof(color[0]));
for (int i = 0;
i < m;
i ++) {
int x, y;
cin >> x>>y;
mp[x].push_back(y);
//无向图存储两条边
mp[y].push_back(x);
}
int flag = true;
//初始化无向图是二分图
for (int i = 1;
i <= n;
i ++) {
if (color[i] == -1 && !bfs(i)) { //对每个连通分支染色,如果两个相邻的点
//颜色相同,则不是二分图。
flag = false;
break;
}
}
if (!flag) {puts("No");
continue;
}
int res = work(n);
printf("%d\n", res/2);
}
return 0;
}
【图论|判断一个无向图是不是二分图】
推荐阅读
- 一个人的旅行,三亚
- 一个小故事,我的思考。
- 一个人的碎碎念
- 七年之痒之后
- 我从来不做坏事
- 异地恋中,逐渐适应一个人到底意味着什么()
- 迷失的世界(二十七)
- live|live to inspire 一个普通上班族的流水账0723
- 遗憾是生活的常态,但孝顺这件事,我希望每一个人都不留遗憾
- NO.38|NO.38 我不是嫁不出去,而是不想嫁