π¦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;
}