BOJ 1967 트리의 지름

1 개요[ | ]

BOJ 1967 트리의 지름

2 C++[ | ]

#include <bits/stdc++.h>
using namespace std;

int n;
vector<pair<int,int>> graph[10001];
bool A[10001] = {};

int dfs(int x) {
	if (A[x]) return -1000;
	int sum = 0;
	A[x] = true;
	for (int i=0; i<graph[x].size(); i++) {
		int node = graph[x][i].first;
		int value = graph[x][i].second;
		sum = max(sum, value + dfs(node));
	}
	return sum;
}

void solve() {
	int answer = 0;
	for (int i=1; i<=n; i++) {
		for (int j=1; j<=n; j++) {
		    A[j] = false;
		}
		answer = max(answer, dfs(i));
	}
	cout << answer;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	int a, b, c;
	for (int i=1; i<n; i++) {
		cin >> a >> b >> c;
		graph[a].push_back({b, c});
		graph[b].push_back({a, c});
	}
    solve();
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}