cgy12306
[백준 BoJ] 1167 - 트리의 지름 본문
// https://www.acmicpc.net/problem/1167
// 트리의 지름
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
vector<pair<int, int>> tree[100001];
bool visited[100001];
int sum = 0, sp;
void dfs(int start, int length) {
visited[start] = true;
if (sum < length) {
sum = length;
sp = start;
}
for (auto next : tree[start]) {
if (!visited[next.first]) {
dfs(next.first, length + next.second);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int V;
cin >> V;
for (int i = 0; i < V; i++) {
int from, to, length;
cin >> from;
while (1) {
cin >> to;
if (to == -1) break;
cin >> length;
tree[from].push_back(make_pair(to, length));
tree[to].push_back(make_pair(from, length));
}
}
dfs(1, 0);
memset(visited, false, sizeof(visited));
dfs(sp, 0);
cout << sum;
}
'Algorithm > C++' 카테고리의 다른 글
[백준 BoJ] 9466 - 텀 프로젝트 (0) | 2021.08.17 |
---|---|
[백준 BoJ] 2146 - 다리 만들기 (0) | 2021.08.17 |
[백준 BoJ] 1967 - 트리의 지름 (0) | 2021.08.10 |
[백준 BoJ] 14675 - 단절점과 단절선 (0) | 2021.08.10 |
[백준 BoJ] 1068 - 트리 (0) | 2021.08.06 |
Comments