Re: [闲聊] 每日leetcode

楼主: dont   2025-01-30 11:05:28
2493. Divide Nodes Into the Maximum Number of Groups
## 思路
每个点做BFS
如果不是bipartite 就回传-1
把遇到的最小点当作GROUP ID 更新GROUP的长度
最后再把每个GROUP的长度加总
## Code
```cpp
class Solution {
public:
int magnificentSets(int n, vector<vector<int>>& edges) {
vector<vector<int>> adjList(n);
for (auto& edge: edges) {
adjList[edge[0]-1].push_back(edge[1]-1);
adjList[edge[1]-1].push_back(edge[0]-1);
}
unordered_map<int, int> distance;
for (int i=0; i<n; ++i) {
queue<int> q({i});
vector<int> group(n, -1);
group[i] = 0;
int level = 0;
int minNode = i;
while(!q.empty()) {
int count = q.size();
while (count

Links booklink

Contact Us: admin [ a t ] ucptt.com