https://www.acmicpc.net/problem/11725
11725๋ฒ: ํธ๋ฆฌ์ ๋ถ๋ชจ ์ฐพ๊ธฐ
๋ฃจํธ ์๋ ํธ๋ฆฌ๊ฐ ์ฃผ์ด์ง๋ค. ์ด๋, ํธ๋ฆฌ์ ๋ฃจํธ๋ฅผ 1์ด๋ผ๊ณ ์ ํ์ ๋, ๊ฐ ๋ ธ๋์ ๋ถ๋ชจ๋ฅผ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
www.acmicpc.net
import java.util.Arrays;
import java.util.Scanner;
class Main {
static class Node {
Integer data;
Node l;
Node r;
Node p;
public Node(int data) {
this.data = data;
this.l = null;
this.r = null;
}
public Node(int data, Node p) {
this.data = data;
this.l = null;
this.r = null;
this.p = p;
}
}
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// ์
๋ ฅ์ ์์๋๋ก ๋ฐ์ผ๋ฉด์, ๋จผ์ ์
๋ ฅ๋ ๋
ธ๋๊ฐ ์กด์ฌํ๋ ์ง ์ฐพ์์ผ๋ ๊ฒ ๊ฐ์.
Node[] tree = new Node[n+1];
tree[1] = new Node(1); // ๋ฃจํธ๋ฅผ 1์ด๋ผ๊ณ ๊ฐ์ ํ์.
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
if (tree[a] == null && tree[b] != null) {
// ์ด๋ฌ๋ฉด ์์ง ๋
ธ๋์ ๋ค์ด์ค์ง์์๊ฒ.
tree[a] = new Node(a);
tree[a].p = tree[b];
if (a < b) { // ์์ผ๋ฉด l, b๊ฐ ๋ถ๋ชจ
tree[b].l = tree[a];
} else {
tree[b].r = tree[a];
}
} else if (tree[a] != null && tree[b] == null) {
tree[b] = new Node(b);
tree[b].p = tree[a];
if (a > b) { // ์์ผ๋ฉด l
tree[a].l = tree[b];
} else {
tree[a].r = tree[b];
}
} else { // ๋๋ค null์ด๋ฉด ๊ทธ๋ฅ ํ๋ ๋ง๋ค๊ธฐ
if (a < b) {
tree[a] = new Node(a);
tree[b] = new Node(b);
tree[b].p = tree[a];
tree[a].r = tree[b];
} else {
tree[a] = new Node(a);
tree[b] = new Node(b);
tree[b].p = tree[a];
tree[a].l = tree[b];
}
}
}
for (int i = 2; i <= n; i++) {
System.out.println(tree[i].p.data);
}
}
}
์ค๋ต๋ ธํธ(ํธ๋ ๋์ ์ฌ๊ณ ๊ณผ์ ๋ฐ ๊ฐ์ ์ )
๋ถ๋ชจ ๋ ธ๋๋ฅผ ๊ฐ๋ฆฌํฌ p๋ฅผ ๋ ธ๋์ ๋ฃ๊ณ ์์ ๋ ธ๋๋ฅผ ๋ง๋ค ๋ ์ฌ์ฉํ ์์ฑ์๋ฅผ ์ถ๊ฐ๋ก ๋ฃ์ด์ ์ง๋ดค๋ค. ์๋ง ์ด๋ฏธ ์์์ด ์๋๋ฐ ๋ ๋ฎ์ด์จ์ npe๊ฐ ๋์ค๋ ๊ฒ ๊ฐ๋ค...
๊ทธ๋์ ๋ ธ๋ ์ถ๊ฐ๋ถ๋ถ์ ์๋์ ๊ฐ์ด ๋ฐ๊ฟ๋ดค๋๋ฐ๋ ์ค๋ฅ๊ฐ ๋์๋ค.
tree[a] = new Node(a);
tree[a].p = tree[b];
if (a < b) { // ์์ผ๋ฉด l, b๊ฐ ๋ถ๋ชจ -> ์์์ด ์ด๋ฏธ ์๋ค๋ฉด? ๊ทธ ์์๊ณผ ๊ฐ ๋น๊ตํ๊ธฐ
if(tree[b].l != null) {
tree[b].r = tree[a];
}else {
tree[b].l = tree[a];
}
} else {
if(tree[b].r != null) {
tree[b].l = tree[a];
}else {
tree[b].r = tree[a];
}
}
์ ๋ต์ฝ๋
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
class Main {
static List<Integer>[] tree;
static int[] p;
static boolean[] v;
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
tree = new ArrayList[n+1];
p = new int[n+1];
v = new boolean[n+1];
for(int i=0; i<n+1; i++) {
tree[i] = new ArrayList<>();
}
for(int i=0; i<n-1; i++) {
int node1 = sc.nextInt();
int node2 = sc.nextInt();
tree[node1].add(node2);
tree[node2].add(node1);
}
find(1); // 1์ด ๋ฃจํธ๋๊น
for(int i=2; i<=n; i++) {
System.out.println(p[i]);
}
}
public static void find(int node) {
v[node] = true;
for(int i=0; i<tree[node].size(); i++) {
int child = tree[node].get(i);
if(!v[child]) {
p[child] = node;
find(child);
}
}
}
}
ArrayList๋ฅผ ์ฌ์ฉํด์ Tree๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์๋ ์๊ตฌ๋๋ผ๋ ๊ฑธ ๋ฐฐ์ด ๋ฌธ์ ๋ค.
Node๋ฅผ ์ฌ์ฉํ์ง์์ผ๋๊น ์ฌ๊ท๋ฅผ ์ด์ฉํด์ ๊ฐ์ ์ถ๊ฐํ๊ณ , ํธ๋ฆฌ ๊ตฌ์กฐ ์ ์ฌ๊ท๋ก ํ์ํ๋ ๊ฒ ์์ํ๋๊น ์ฌ๊ท๋ก ์์ ๋ ธ๋๋ฅผ ์ป๊ณ , ๊ทธ ์์๋ ธ๋์ ๋ถ๋ชจ ๋ ธ๋๊ฐ์ ์ ์ฅํ p ๋ฐฐ์ด์ ๋ง๋ค์ด์ ํ์ฌ ๋ ธ๋๊ฐ์ p์ ์์๋ ธ๋๊ฐ idx์ ์ ์ฅํ๋ค.
์๊ณ ์ผ์ด๋์ ๋ ํ์ด๋ณผ ๋ฌธ์ .
๋์์ด ๋๋ค๋ฉด ๋๊ธ์ด๋ ๊ณต๊ฐ ๋ฒํผ ํ ๋ฒ์ฉ ๋๋ฅด๊ณ ๊ฐ์ฃผ์ธ์! ๋ก๊ทธ์ธ ์ํด๋ ๋ฉ๋๋ค ^_^
'OJ๐ผ > ์ค๋ต๋ ธํธ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ][JAVA] 1918๋ฒ: ํ์ ํ๊ธฐ์ (0) | 2024.02.17 |
---|---|
[BOJ][JAVA] 14267๋ฒ: ํ์ฌ ๋ฌธํ 1 (0) | 2024.02.12 |
[BOJ][JAVA] 1012๋ฒ: ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2024.02.07 |
[BOJ][JAVA] 15988: 1, 2, 3 ๋ํ๊ธฐ 3 (0) | 2024.01.28 |
[BOJ][JAVA] 2910๋ฒ: ๋น๋์ ๋ ฌ (0) | 2024.01.27 |