02
10

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์— ์ €์žฅํ•œ๋‹ค.

์ž๊ณ  ์ผ์–ด๋‚˜์„œ ๋˜ ํ’€์–ด๋ณผ ๋ฌธ์ œ.

 

 

๋„์›€์ด ๋๋‹ค๋ฉด ๋Œ“๊ธ€์ด๋‚˜ ๊ณต๊ฐ ๋ฒ„ํŠผ ํ•œ ๋ฒˆ์”ฉ ๋ˆ„๋ฅด๊ณ  ๊ฐ€์ฃผ์„ธ์š”! ๋กœ๊ทธ์ธ ์•ˆํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค ^_^

 

๋ฐ˜์‘ํ˜•
COMMENT