πŸ“¦1967: TreeDiameter


[μ•Œκ³ λ¦¬μ¦˜ 풀이] κ°•λ™κ·œ
Reviewed by Kade Kang (devkade12@gmail.com)
Reviewed:: 2024-02-08 문제 ν™•μΈν•˜κΈ°

풀이

  • DFS λ₯Ό 2번 μ‚¬μš©ν•˜λŠ” 방식이닀.
  • κ΅¬μŠ¬λ§ˆλ‹€ μ„œλ‘œ λ‹€λ₯Έ 길이의 μ‹€λ‘œ 맀달아놓고 ν•œ κ΅¬μŠ¬μ„ 선택해 μœ„λ‘œ λ“€μ–΄μ˜¬λ € λŠ˜μ–΄λœ¨λ¦°λ‹€.
  • κ°€μž₯ 거리가 λ¨Ό κ΅¬μŠ¬μ„ 선택해 λ‹€μ‹œ ν•œ 번 λŠ˜μ–΄λœ¨λ¦¬λ©΄ 트리의 지름이 λ‚˜μ˜¨λ‹€.
#include <iostream>
#include <vector>
#include <stack>
#define endl '\n';
using namespace std;
 
int V;
vector<pair<int, int>> vec[10001];
stack<pair<int, int>> st;
int vis[10002], res = 0;
 
void Initialize(){
    fill(vis, vis+10002, 0);
}
 
void Input(){
    cin >> V;
    for(int i = 1; i < V; i++){
        int start, end, dist;
        cin >> start >> end >> dist;
        vec[start].push_back({end, dist});
        vec[end].push_back({start, dist});
    }
}
 
int dfs(int start){
    st.push({start, 0});
    vis[start] = 1;
 
    int max_v = 0, max_dist = 0;
    
    while(!st.empty()){
        auto info = st.top(); st.pop();
        int prev_v = info.first;
        int prev_dist = info.second;
 
        if(max_dist < prev_dist){
            max_dist = prev_dist;
            max_v = prev_v;
        }
 
        for(auto nxt : vec[prev_v]){
            int cand_v = nxt.first;
            int cand_dist = nxt.second;
            if (vis[cand_v] == 1 || prev_v == cand_v) continue;
 
            st.push({cand_v, prev_dist + cand_dist});
            vis[cand_v] = 1;
        }
    }
    if(res < max_dist) res = max_dist;
    return max_v;
}
 
void Solution(){
    int max_v = dfs(1);
    Initialize();
    max_v = dfs(max_v);
    cout << res << endl;
}
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    Input();
    Solution();
 
    return 0;
}