#include <stdio.h> #include <iostream> #include <vector> #include <queue> #include <map> #include <math.h> #include <string> #include <string.h> #include <stdlib.h> using namespace std; int N; vector<pair<int, int>> graph[100010]; int visited[100010]; int Max; int MaxNode; void BFS(int start) { queue<int> q; q.push(start); visited[start] = 0; while(!q.empty()) { start = q.front(); q.pop(); for(int i = 0; i < graph[start].size(); i++) { if(visited[graph[start][i].first] == -1) { visited[graph[start][i].first] = visited[start] + graph[start][i].second; q.push(graph[start][i].first); if(Max < visited[graph[start][i].first]) { Max = visited[graph[start][i].first]; MaxNode = graph[start][i].first; } } } } } int main(void) { // freopen("B1167_input.txt", "r", stdin); cin >> N; for(int i = 1; i <= N; i++) { int from, to, weight; cin >> from; while(1) { cin >> to; if(to == -1) { break; } else { cin >> weight; } graph[from].push_back({to, weight}); } } // 루트에서 가장 먼 노드를 찾음 memset(visited, -1, sizeof(visited)); BFS(1); // 위에서 찾은 먼 노드에서 가장 먼 노드를 찾음 memset(visited, -1, sizeof(visited)); BFS(MaxNode); cout << Max; return 0; } | cs |
'Baekjoon > BFS' 카테고리의 다른 글
[백준 1938] 통나무 옮기기 (BFS) (C/C++) (★★★) (0) | 2020.02.19 |
---|---|
[백준 1261] 알고스팟 (BFS) (C/C++) (★★) (0) | 2020.02.16 |
[백준 1967] 트리의 지름 (BFS, Tree) (C/C++) (0) | 2020.02.16 |
[백준 2146] 다리 만들기 (BFS) (C/C++) (0) | 2020.01.28 |
[백준 2178] 미로 탐색 (BFS) (C/C++) (0) | 2020.01.28 |