Home BOJ. Lowest Common Ancestor (3584)
Post
Cancel

BOJ. Lowest Common Ancestor (3584)

[Link] https://www.acmicpc.net/problem/3584


Common Dynamic Progrmming Solution


Common Dynamic Progrmming Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.*;
import java.io.*;

public class Main {
	static BufferedReader br;
	static StringBuilder sb = new StringBuilder();
	static int[] containsV1;
	static int[] containsV2;
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		int t = toi(br.readLine());
		Node[] nodes;
		int[] arr;

		for(int iter = 0; iter < t; iter++) {
			int n = toi(br.readLine());
			nodes = new Node[n];
			for(int i = 0; i < n; i++) nodes[i] = new Node(i);
			containsV1 = new int[n];
			containsV2 = new int[n];
			Arrays.fill(containsV1, -1);
			Arrays.fill(containsV2, -1);

			for(int i = 0; i < n - 1; i++) {
				arr = getArr();
				int a = arr[0] - 1, b = arr[1] - 1;
				nodes[a].list.add(nodes[b]);
				nodes[b].parent = a;
			}

			int rootIdx = 0;
			for(int i  = 0; i < n; i++) {
				if(nodes[i].parent == -1) {
					rootIdx = i;
					break;
				}
			}
			arr = getArr();
			sb.append(getCommonParent(nodes[rootIdx], arr[0] - 1, arr[1] - 1) + 1).append("\n");
		}
		print(sb);
	}

	static int getCommonParent(Node node, int a, int b) {
		int store = 0;
		while(true) {
			boolean next = false;
			for(Node child: node.list) {
				if(contains1(child, a) == 1 && contains2(child, b) == 1) {
					node = child;
					next = true;
				}
			}
			if(!next) break;
		} return node.idx;

	}

	static int contains1(Node node, int idx) {
		if(containsV1[idx] != -1) return containsV1[node.idx];
		if(node.idx == idx) return containsV1[node.idx] = 1;
		for(Node e : node.list) {
			if(contains1(e, idx) == 1) return containsV1[node.idx] = 1;
		}
		return containsV1[node.idx] = 0;
	}

	static int contains2(Node node, int idx) {
		if(containsV2[idx] != -1) return containsV2[node.idx];
		if(node.idx == idx) return containsV2[node.idx] = 1;
		for(Node e : node.list) {
			if(contains2(e, idx) == 1) return containsV2[node.idx] = 1;
		}
		return containsV2[node.idx] = 0;
	}

	static class Node {
		int idx;
		int parent = -1;
		ArrayList<Node> list = new ArrayList<>();
		public Node(int idx) { this.idx = idx; }
	}

	static int toi(String s) { return Integer.parseInt(s); }
	static long tol(String s) { return Long.parseLong(s); }
	static String[] getLine() throws IOException { return br.readLine().split(" "); }
	static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
	static <T> void print(T s) { System.out.print(s); }
	static <T> void println(T s) { System.out.println(s); }
}
This post is licensed under CC BY 4.0 by the author.