AcWing 860. 染色法判定二分图
原创 已于 2024-02-27 11:15:42 修改 · 粉丝可见 · 418 阅读 · 5 · 4 · 本内容遵循CC 4.0 BY-SA版权协议 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 GEO检测 · 编辑
文章链接:https://blog.csdn.net/hacker_51/article/details/136315717
本题链接: 活动 - AcWing
题目:

样例:
cobol<br/>4 4<br/>1 3<br/>1 4<br/>2 3<br/>2 4<br/> |
思路:
根据题目意思,我们明确一下二分图的含义。
二分图是图论中的一个重要概念。一个图被称为二分图,当且仅当能够将其所有顶点分割成两个互不相交的子集,使得每条边的两个顶点分别属于不同的子集。
换句话说,对于一个二分图,可以把图中的顶点分成两组,使得同一组内的顶点之间没有边相连,而不同组内的顶点之间有边相连。
通俗的讲,二分图就像是一个人群中的男女混合舞会,所有参与者可以分成两组:男生和女生。在这个舞会上,男生只和女生跳舞,而不和其他男生跳舞;女生也只和男生跳舞,而不和其他女生跳舞。没有两个男生或两个女生会成对跳舞。
如果一个图是二分图,就意味着可以将所有的点分成两组,使得同一组内的点之间没有直接相连的边,而不同组内的点之间有直接相连的边。这种特性在很多实际问题中都有重要的应用。
所以,我们可以用DFS或者BFS暴搜即可枚举出答案。
代码详解如下:
DFS法:
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include <iostream> #include <vector> #include <queue> #include <cstring> #include <algorithm> #include <unordered_map> #define endl '\n' #define int long long #define Yes puts("Yes") #define No puts("No") #define umap unordered_map #define All(x) x.begin(),x.end() #pragma GCC optimize(3,"Ofast","inline") #define IOS std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0) using namespace std; const int N = 2e6 + 10; int n,m;
vector<int>h(N,-1); int e[N],ne[N],idx; inline void Add(int a,int b) { e[idx] = b,ne[idx] = h[a],h[a] = idx++; } int color[N];
bool DFS(int node,int inColor) { color[node] = inColor; for(int i = h[node];i != -1;i = ne[i]) { int j = e[i]; if(!color[j]) { if(!DFS(j,3 - inColor)) return false; }else if(inColor == color[j]) return false; } return true; } inline void solve() { cin >> n >> m; while(m--) { int a,b; cin >> a >> b; Add(a,b),Add(b,a); } for(int i = 1;i <= n;++i) { if(!color[i]) { if(!DFS(i,1)) { No; return ; } } } Yes; } signed main() {
IOS; int _t = 1;
while (_t--) { solve(); } return 0; }
|
最后提交:

BFS法:
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #include <iostream> #include <vector> #include <queue> #include <cstring> #include <algorithm> #include <unordered_map> #define endl '\n' #define int long long #define Yes puts("Yes") #define No puts("No") #define umap unordered_map #define All(x) x.begin(),x.end() #pragma GCC optimize(3,"Ofast","inline") #define IOS std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0) using namespace std; const int N = 2e6 + 10; using PII=pair<int,int>; int n,m;
vector<int>h(N,-1); int e[N],ne[N],idx; inline void Add(int a,int b) { e[idx] = b,ne[idx] = h[a],h[a] = idx++; } int color[N];
inline bool BFS() { queue<PII>q; for(int i = 1;i <= n;++i) { if(!color[i]) { q.emplace(PII(i,1)); while(q.size()) { PII now = q.front(); q.pop(); int node = now.first; int inColor = now.second; color[node] = inColor; for(int i = h[node];i != -1;i = ne[i]) { int j = e[i]; if(!color[j]) { q.emplace(PII(j,3 - inColor)); }else if(color[j] == inColor) return false; } } } } return true; } inline void solve() { cin >> n >> m; while(m--) { int a,b; cin >> a >> b; Add(a,b),Add(b,a); } if(BFS()) Yes; else No; } signed main() {
IOS; int _t = 1;
while (_t--) { solve(); } return 0; }
|
最后提交:
