Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
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
Tags
more
Archives
Today
Total
관리 메뉴

자이의 프로그래밍

가장 먼 노드 본문

Algorithm/Cases-Study

가장 먼 노드

Xi_kor 2021. 6. 29. 19:13

큐를 이용해서 풀었다. 일반적인 방법!

#include <string>
#include <vector>
#include <queue>
using namespace std;

vector <int> v[20100];
int ch[20100];

int solution(int n, vector<vector<int>> edge) {
    int answer = 0;
    
    for(int i=0; i<edge.size(); i++){
        int x=edge[i][0];
        int y=edge[i][1];
        v[x].push_back(y);
        v[y].push_back(x);
    }
    ch[1]=1;
    queue <int> q;
    q.push(1);
    int maxi=-1;
    
    while(!q.empty()){
        int x=q.front();
        q.pop();
        if(ch[x]>maxi) maxi=ch[x];
        
        for(int i=0; i<v[x].size(); i++){
            if(ch[v[x][i]]==0){
                q.push(v[x][i]);
                ch[v[x][i]]=ch[x]+1;
            }
        }
    }
    
    for(int i=2; i<=n; i++){
        if(ch[i]==maxi) answer++;
    }
    return answer;
}

'Algorithm > Cases-Study' 카테고리의 다른 글

스타트와 링크  (0) 2021.07.10
단어수학  (0) 2021.07.10
더 맵게  (0) 2021.06.29
점프 1890  (0) 2021.05.13
물통 2251  (0) 2021.05.13