2020/07/15 - [알고리즘 공부] - boj 11437: LCA
두 정점간의 거리는 LCA를 사용해서 구할 수 있다.
먼저 LCA를 구한 뒤, LCA까지의 거리를 모두 더하면 된다.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] depth = new int[n + 1];
int[] parentNode = new int[n + 1];
int[] distances = new int[n + 1];
boolean[] isVisited = new boolean[n + 1];
LinkedList<Node> pair[] = new LinkedList[n + 1];
for (int i = 1; i <= n; i++) {
pair[i] = new LinkedList<Node>();
}
for (int i = 1; i < n; i++) {
int node1 = in.nextInt();
int node2 = in.nextInt();
int distance = in.nextInt();
pair[node1].add(new Node(node2, distance));
pair[node2].add(new Node(node1, distance));
}
depth = calculateNodeDepth(pair, parentNode, distances, n);
int test = in.nextInt();
for (int t = 0; t < test; t++) {
int node1 = in.nextInt();
int node2 = in.nextInt();
int depth1 = depth[node1];
int depth2 = depth[node2];
int distance = 0;
if (depth1 < depth2) {
int tmp = node1;
node1 = node2;
node2 = tmp;
depth1 = depth[node1];
depth2 = depth[node2];
}
distance = calculateLCA(parentNode, depth, distances, node1, node2);
System.out.println(distance);
}
}
public static int calculateLCA(int parent[], int depth[], int distance[], int node1, int node2) {
int depth1 = depth[node1];
int depth2 = depth[node2];
int ans = 0;
while (depth1 > depth2) {
ans += distance[node1];
node1 = parent[node1];
depth1 = depth[node1];
}
while (node1 != node2) {
ans += distance[node1];
ans += distance[node2];
node1 = parent[node1];
node2 = parent[node2];
}
return ans;
}
public static int[] calculateNodeDepth(LinkedList<Node> pair[], int parent[], int distance[], int n) {
Queue<Node> queue = new LinkedList<Node>();
int depth[] = new int[n + 1];
boolean isVisited[] = new boolean[n + 1];
queue.add(new Node(1, 0));
depth[1] = 0;
isVisited[1] = true;
while (!queue.isEmpty()) {
Node node = queue.poll();
for (Node i : pair[node.name]) {
if (!isVisited[i.name]) {
queue.add(i);
depth[i.name] = depth[node.name] + 1;
parent[i.name] = node.name;
distance[i.name] = i.distance;
isVisited[i.name] = true;
}
}
}
return depth;
}
}
class Node {
int name;
int distance;
public Node(int name, int distance) {
this.name = name;
this.distance = distance;
}
}
'알고리즘 공부 > boj' 카테고리의 다른 글
boj 10986: 나머지 합 (0) | 2020.07.18 |
---|---|
boj 11003: 최솟값 찾기 (0) | 2020.07.18 |
boj 11437: LCA (0) | 2020.07.15 |
boj 2143: 두 배열의 합 (0) | 2020.07.15 |
boj 2632: 피자판매 (0) | 2020.07.15 |