二分图交叉染色法模版题

二分图交叉染色法

HDU 4751

Problem Description

Input

Output

Sample Input

Sample Output

题目要求

输入互相认识人的编号,问可不可以把人群分成认识和不认识的、

参考AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<string.h>
#include<vector>
using namespace std;
vector<int>e[150];
int u;
bool flag;
int color[150],map[150][150];
bool DFS(int v,int c)
{
color[v]=c;
for(int i=0;i<e[v].size();i++)
{
if(color[e[v][i]]==c)
return false;
if(color[e[v][i]]==0&&!DFS(e[v][i],-c))
return false;
}
return true;
}
int main()
{
int n;
while(cin>>n)
{
for(int i=1;i<=n;i++) e[i].clear();
flag=true;
memset(map,0,sizeof(map));
memset(color,0,sizeof(color));
for(int i=1;i<=n;i++)
while(cin>>u&&u)
map[i][u]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(j==i)
continue;
if(map[i][j]==0)
{
e[i].push_back(j);
e[j].push_back(i);
}
}
for(int i=1;i<=n&&flag;i++)
if(color[i]==0)
flag=DFS(i,1);
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}

思路

使用边集数组map作为过度作用,从而把互不认识的人加入vector前向星中。DFS中使用交叉染色法。

文章目录
  1. 1. 二分图交叉染色法
    1. 1.1. HDU 4751
      1. 1.1.1. 题目要求
      2. 1.1.2. 参考AC代码
      3. 1.1.3. 思路
|