Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <queue>
- using namespace std;
- struct arco_t {
- int u, v, w;
- };
- int N;
- vector<vector<arco_t>> G;
- int bfs(int start) {
- vector<int> distances(N+1, -1);
- vector<bool> visited(N+1, false);
- queue<int> coda;
- coda.push(start);
- distances[start] = 0;
- int far = 0;
- while (!coda.empty()) {
- int nodo = coda.front();
- coda.pop();
- visited[nodo] = true;
- for (auto x : G[nodo]) {
- if (visited[x.v]) continue;
- int cost = x.w + distances[nodo];
- far = max(cost, far);
- distances[x.v] = cost;
- coda.push(x.v);
- }
- }
- return far;
- }
- int main() {
- ifstream cin("input.txt");
- ofstream out("output.txt");
- cin >> N;
- G.resize(N+1);
- for (int i = 1; i < N; i++) {
- int a, b, l;
- cin >> a >> b >> l;
- G[a].push_back({a, b, l});
- G[b].push_back({b, a, l});
- }
- int longest = 0;
- for (int i = 1; i <= N; i++) {
- longest = max(bfs(i), longest);
- }
- out << longest;
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement