๐ฆ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;
}