기본 콘텐츠로 건너뛰기

백준알고리즘 1167번, 트리의 지름 풀이

트리의 지름은 두 vertex간의 길이가 가장 긴 값입니다. 
이 문제에서는 간선에 가중치가 있기 때문에 멀어도 길이가 짧은 수 있고 그 반대가 될 수도 있습니다.
트리의 지름을 구하는 방법은 

1. 임의의 정점으로 부터 가장 먼 정점을 구한다.
2. 1에서 나온 정점으로 부터 가장 먼 정점을 구한다.
3. 1과 2에서 나온 정점간의 거리가 트리의 지름!

이 방법은 공식처럼 외우시면 됩니다. 증명은 구글링 하시면 잘 나옵니다.

저는 큐를 이용해 BFS로 풀었습니다. BFS로 계속 풀어서 그런지 이게 편하네요.
임의의 정점을 1번노드로 잡아서 BFS를 한번 수행하고, 거기서 나온 정점으로 한번 더 수행해서 구했습니다.


-----------------------------------------------------------------------------------

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <functional>
#include <utility>

using namespace std;

typedef pair<int, int> P; //노드, 거리

int main(void) {
int V, u, v, w;
vector<P> adj[100001];
bool visited[100001] = { false };
int cost[100001] = { 0 };
queue<int> q;
int curr, next, weight, max_weight=0,max_node;

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif

scanf("%d\n", &V);
for (int i = 0; i < V; i++) {
scanf("%d", &u);
do
{
scanf("%d", &v);
if (v != -1) {
scanf("%d", &w);
adj[u].push_back(P(v, w));
}

} while (v!=-1);
}

q.push(1); //1번 노드를 기준으로 BFS
while (!q.empty()) {
curr = q.front();
q.pop();
visited[curr] = true;
for (int i = 0; i < adj[curr].size(); i++) {
next = adj[curr][i].first;
if (visited[next] == false) {
weight = adj[curr][i].second;
cost[next] = cost[curr] + weight;
if (cost[next] > max_weight) {
max_weight = cost[next];
max_node = next;
}
q.push(next);
}
}
}

memset(visited, 0, sizeof visited);
memset(cost, 0, sizeof cost);

q.push(max_node); // 위의 BFS수행에서 나온 노드를 바탕으로 한번더 수행
while (!q.empty()) {
curr = q.front();
q.pop();
visited[curr] = true;
for (int i = 0; i < adj[curr].size(); i++) {
next = adj[curr][i].first;
if (visited[next] == false) {
weight = adj[curr][i].second;
cost[next] = cost[curr] + weight;
if (cost[next] > max_weight) {
max_weight = cost[next];
max_node = next;
}
q.push(next);
}
}
}

printf("%d\n", max_weight);


#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif

return 0;

}

댓글

이 블로그의 인기 게시물

2017 암호경진대회 4번

가장 쉽게 풀었죠. 30분도 안걸렸던거 같네요. 프로그램을 통해서 암호문의 유효성이 검증되는데요, 역공학을 통해서 암호문을 쉽게 찾을 수 있습니다. 답안  : Snow White and the Seven Dwarfs!. 풀이  : Oracle 함수가  CRC 를 계산하기 위해서는 중간에 평문이 복구될 것으로 보였다 .  제공하는  dll 을 이용하여  Oracle 함수의 입력으로 암호문을 넣은 프로그램을 작성하였다 . IDA 와  Immunity Debugger 를 이용해 프로그램을 역공학하여 얻을 수 있었다 .