Problem Solving/BOJ [백준 1167번] [트리] 트리의 지름 - 728x90 반응형 1967과 동일한 알고리즘으로 접근해주면 된다. 구현 방법에 대해서 잘 기억해두도록 하자. #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; struct node{ vector<pair<int, int > > len_store; }; int visited[100001] = {0}; int end_point; int result; void tree_checker(node *data_store, int start, int length){ if(visited[start] != 0) return; visited[start] = 1; if(result < length){ end_point = start; result = length; } for(int i = 0; i < data_store[start].len_store.size(); i++){ tree_checker(data_store, data_store[start].len_store[i].first, length + data_store[start].len_store[i].second); } return; } int main(void){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); //간선의 갯수 int n; cin >> n; node data_store[n + 1]; for(int i = 0; i < n; i++){ int num; cin >> num; int node; while(true){ cin >> node; if(node == -1) break; else{ int length; cin >> length; data_store[num].len_store.push_back(make_pair(node,length)); data_store[node].len_store.push_back(make_pair(num, length)); } } }// data store // Find end_point tree_checker(data_store, 1, 0); // Reinitialize visited array fill(visited, visited + 100001, 0); // Find result tree_checker(data_store, end_point, 0); cout << result << "\n"; return 0; } 반응형 공유하기 URL 복사카카오톡 공유페이스북 공유엑스 공유 게시글 관리 구독하기비룡의 컴퓨터이야기 Contents 당신이 좋아할만한 콘텐츠 [백준 13900번] [구현] 순서쌍의 곱의 합 2021.01.02 [백준 1952번] [구현] 달팽이 2 2021.01.02 [백준 5639번] [트리] 이진 검색 트리 2020.11.24 [백준 1967번] [트리] 트리의 지름 2020.11.23 댓글 0 + 이전 댓글 더보기