"프로그래머스 258711 도넛과 막대 그래프"의 두 판 사이의 차이

(새 문서: ==개요== {{프로그래머스|레벨=2|페이지=1|분류=2024 KAKAO WINTER INTERNSHIP}} ==C++== <syntaxhighlight lang='cpp'> #include <vector> #include <map> #include <algorithm>...)
 
 
25번째 줄: 25번째 줄:
         maxNode = max({maxNode, from, to});
         maxNode = max({maxNode, from, to});
     }
     }
     for (int node = 1; node <= maxNode; ++node) {
     for (int node = 1; node <= maxNode; node++) {
         const auto& d = degrees[node];
         const auto& d = degrees[node];
         if (d.out >= 2 && d.in == 0) {
         if (d.out >= 2 && d.in == 0) {

2024년 1월 26일 (금) 01:06 기준 최신판

1 개요[ | ]

프로그래머스 258711 도넛과 막대 그래프

2 C++[ | ]

#include <vector>
#include <map>
#include <algorithm>

using namespace std;

struct NodeDegrees {
    int out = 0;
    int in = 0;
};

vector<int> solution(vector<vector<int>> edges) {
    vector<int> answer(4, 0);
    map<int, NodeDegrees> degrees;
    int maxNode = 0;
    for (const auto& e : edges) {
        int from = e[0], to = e[1];
        degrees[from].out++;
        degrees[to].in++;
        maxNode = max({maxNode, from, to});
    }
    for (int node = 1; node <= maxNode; node++) {
        const auto& d = degrees[node];
        if (d.out >= 2 && d.in == 0) {
            answer[0] = node;
        } else if (d.out == 0) {
            answer[2]++;
        } else if (d.out >= 2 && d.in >= 2) {
            answer[3]++;
        }
    }
    answer[1] = degrees[answer[0]].out - (answer[2] + answer[3]);
    return answer;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}