LeetCode #2493

Divide Nodes Into the Maximum Number of Groups

1개의 풀이 · C++

문제 원문 보기 ↗

SOLUTION INFO

C++ · main.cpp

main.cpp
class Solution {
public:
    int magnificentSets(int N, vector<vector<int>>& edges) {
        vector<vector<int>> G(N);
        for(const auto &e: edges) {
            G[e[0] - 1].push_back(e[1] - 1);
            G[e[1] - 1].push_back(e[0] - 1);
        }
        vector<int> D(N);
        for(int i = 0; i < N; ++i) {
            queue<int> Q; Q.push(i);
            vector<int> dist(N); dist[i] = 1;
            int mx = 1, root = i;
            while(!Q.empty()) {
                int cur = Q.front(); Q.pop();
                root = min(root, cur);
                for(const int &nxt: G[cur]) {
                    if(dist[nxt] == 0) {
                        dist[nxt] = dist[cur] + 1;
                        mx = max(mx, dist[nxt]);
                        Q.push(nxt);
                    }
                    else if(abs(dist[nxt] - dist[cur]) != 1) return -1;
                }
            }
            D[root] = max(D[root], mx);
        }
        return accumulate(D.begin(), D.end(), 0);
    }
};

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.