-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagnificentSets.cpp
More file actions
59 lines (55 loc) · 2.04 KB
/
magnificentSets.cpp
File metadata and controls
59 lines (55 loc) · 2.04 KB
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
class Solution {
public:
int magnificentSets(int n, vector<vector<int>> &edges) {
vector<vector<int>> g(n);
for (auto &e : edges) {
int x = e[0] - 1, y = e[1] - 1;
g[x].push_back(y);
g[y].push_back(x);
}
int time[n], clock = 0; // time 充当 vis 数组的作用(避免在 BFS 内部重复创建 vis 数组
memset(time, 0, sizeof(time));
auto bfs = [&](int start) -> int {
int depth = 0;
time[start] = ++clock;
vector<int> q = {start};
while (!q.empty()) {
vector<int> nxt;
for (int x : q)
for (int y : g[x])
if (time[y] != clock) { // 没有在同一次 BFS 中访问过
time[y] = clock;
nxt.push_back(y);
}
q = move(nxt);
++depth;
}
return depth;
};
int8_t color[n]; memset(color, 0, sizeof(color));
vector<int> nodes;
function<bool(int, int8_t)> is_bipartite = [&](int x, int8_t c) -> bool {
nodes.push_back(x);
color[x] = c;
for (int y : g[x])
if (color[y] == c || color[y] == 0 && !is_bipartite(y, -c))
return false;
return true;
};
int ans = 0;
for (int i = 0; i < n; ++i) {
if (color[i]) continue;
nodes.clear();
if (!is_bipartite(i, 1)) return -1;
int max_depth = 0;
for (int x : nodes)
max_depth = max(max_depth, bfs(x));
ans += max_depth;
}
return ans;
}
};
// 作者:灵茶山艾府
// 链接:https://leetcode.cn/problems/divide-nodes-into-the-maximum-number-of-groups/solutions/2005131/mei-ju-qi-dian-pao-bfs-by-endlesscheng-s5bu/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。