DMOJ · treepractice1

Tree Practice 1

This C++ solution uses shortest paths for DMOJ treepractice1 Tree Practice 1. Read the reasoning, inspect the code, or try your own test case below.

treepractice1Graphs & treesShortest pathsC++60 lines
Solution226of 248
Open official problem ↗ Download C++ file ↓ Search the library → Open full Code Lab ↗ Report an issue ↗

Approach

Shortest paths

Tree Practice 1 matches the weighted tree and required diameter and radius outputs.

Graphs & trees

Problem and code

Useful links.

Written by benbenyaojifen. Try the problem first, then compare your approach with the code.

Open official problem ↗View exact source file ↗
Implementation

tree_tasks.cpp

C++

    #include <bits/stdc++.h>
    using namespace std;
    const long long INF = 4e18;
    int n;
    vector<vector<pair<int, int>>> adj;
    vector<long long> dijkstra(int start) {
        vector<long long> dist(n + 1, INF);
        priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
        dist[start] = 0;
        pq.push({0, start});
        while (!pq.empty()){
            auto cur = pq.top();
            pq.pop();
            long long d = cur.first;
            int u = cur.second;
            if (d != dist[u]) continue;
            for (auto edge : adj[u]){
                int v = edge.first;
                int w = edge.second;
                long long nd = d + w;
                if (nd < dist[v]) {
                    dist[v] = nd;
                    pq.push({nd, v});
                }
            }
        }
        return dist;
    }
    int main(){
        ios::sync_with_stdio(0);
        cin.tie(0);
        cin >> n;
        adj.assign(n + 1, {});
        for (int i = 0; i < n - 1; i++){
            int u, v, w;
            cin >> u >> v >> w;
            adj[u].push_back({v, w});
            adj[v].push_back({u, w});
        }
        vector<long long> dist1 = dijkstra(1);
        int A = 1;
        for (int i = 1; i <= n; i++){
            if (dist1[i] > dist1[A]) A = i;
        }
        vector<long long> distA = dijkstra(A);
        int B = A;
        for (int i = 1; i <= n; i++){
            if (distA[i] > distA[B]) B = i;
        }
        long long diameter = distA[B];
        vector<long long> distB = dijkstra(B);
        long long radius = INF;
        for (int i = 1; i <= n; i++) {
            long long ecc = max(distA[i], distB[i]);
            if (ecc < radius) radius = ecc;
        }
        cout << diameter << "\n" << radius << "\n";
        return 0;
    }
     
        

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗Keep studying →

Test this problem

Run your code here.

Paste your code, run a test case, compare the output, or trace selected values.

Full trace, comparison & stress testing ↗
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.

Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.