01-09 18:44
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[BAEKJOON] ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ 11725:: ํŠธ๋ฆฌ์˜ ๋ถ€๋ชจ ์ฐพ๊ธฐ ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ 11725:: ํŠธ๋ฆฌ์˜ ๋ถ€๋ชจ ์ฐพ๊ธฐ

miinsun 2022. 4. 5. 00:41

 

๐Ÿ’ฌ  ๋ฌธ์ œ ์„ค๋ช…

๋ฃจํŠธ ์—†๋Š” ํŠธ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋•Œ, ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ๋ฅผ 1์ด๋ผ๊ณ  ์ •ํ–ˆ์„ ๋•Œ, ๊ฐ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

๐Ÿ”จ  ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ 

  •  ์ฒซ์งธ ์ค„์— ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ N (2 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค.
  • ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N-1๊ฐœ์˜ ์ค„์— ํŠธ๋ฆฌ ์ƒ์—์„œ ์—ฐ๊ฒฐ๋œ ๋‘ ์ •์ ์ด ์ฃผ์–ด์ง„๋‹ค.
 

์ถœ๋ ฅ

  • ์ฒซ์งธ ์ค„๋ถ€ํ„ฐ N-1๊ฐœ์˜ ์ค„์— ๊ฐ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ ๋…ธ๋“œ ๋ฒˆํ˜ธ๋ฅผ 2๋ฒˆ ๋…ธ๋“œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1)

7
1 6
6 3
3 5
4 1
2 4
4 7

 

์˜ˆ์ œ ์ถœ๋ ฅ 1)

4
6
1
3
1
4

 

์˜ˆ์ œ ์ž…๋ ฅ 2)

12
1 2
1 3
2 4
3 5
3 6
4 7
4 8
5 9
5 10
6 11
6 12

 

์˜ˆ์ œ ์ถœ๋ ฅ 2)

1
1
2
3
3
4
4
5
5
6
6

โ€‹

๐Ÿ’ป  Main.java

  • ์ฒ˜์Œ์—๋Š” ๋ฌธ์ œ์ด๋ฆ„์„ ๋ณด๊ณ  ํŠธ๋ฆฌ๋กœ ํ‘ธ๋Š”์ง€ ์•Œ๊ณ  ์•„๋ฌด ์˜์‹ฌ์—†์ด ํŠธ๋ฆฌ๋กœ ํ’€์—ˆ๋‹ค.
  • ํ•˜์ง€๋งŒ ์ด ๋ฌธ์ œ๋Š” ํŠธ๋ฆฌ๋กœ ํ’€๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋– , DFS๋‚˜ BFS๋กœ ํ’€์ดํ•ด์•ผํ•œ๋‹ค... ใ… 
  • ์•„๋ž˜ ์ฝ”๋“œ๋Š” ํŠธ๋ฆฌ๋กœ ํ‘ผ ๋ฐฉ๋ฒ•๊ณผ, ๋ฐฉ๋ฒ• ๋ชจ๋‘ ์˜ฌ๋ ธ๋‹ค.

ํŠธ๋ฆฌ๋กœ ํ‘ผ ๋ฐฉ๋ฒ• (์‹œ๊ฐ„ ์ดˆ๊ณผ)

/* ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ - 11725 :: ํŠธ๋ฆฌ์˜ ๋ถ€๋ชจ ์ฐพ๊ธฐ */

import java.util.*;
import java.io.*;

// ๋…ธ๋“œ
class Node { 
	public int data; 
	Node lt, rt, ut;
	
	Node (int data){ 
		this.data = data;
	} 
	Node (int data, Node ut){ 
		this.data = data;
		this.ut = ut;
	} 
}
	
class Tree{
	Node root = new Node (1);
	StringBuilder sb = new StringBuilder();

	// ์ž…๋ ฅ
	void add(Node root, int a, int b) {
		// ๋…ธ๋“œ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด ์ข…๋ฃŒ
		if(root == null) return;
		else if(root.data == a) { // ์ฐพ๊ณ  ์žˆ๋˜ ๋…ธ๋“œ๋ฅผ ์ฐพ์œผ๋ฉด ์ •๋ณด ์ž…๋ ฅ
			if(root.rt == null)
				root.rt = new Node(b, root);
			else
				root.lt = new Node(b, root);
		}
		else if(root.data == b) {
			if(root.rt == null)
				root.rt = new Node(a, root);
			else
				root.lt = new Node(a, root);
		}
		else { // ์™ผ์ชฝ ํฌ์ธํ„ฐ, ์˜ค๋ฅธ์ชฝ ํฌ์ธํ„ฐ๋กœ ๋…ธ๋“œ ์ฐพ๊ธฐ
			add(root.lt, a, b);
			add(root.rt, a, b);
		}
	}
	
	void search(Node root, int a) {
		if(root == null) return;
		if(root.data == a) { // ์ฐพ๊ณ  ์žˆ๋˜ ๋…ธ๋“œ๋ฅผ ์ฐพ์œผ๋ฉด ์ •๋ณด ์ž…๋ ฅ
			sb.append(root.ut.data).append('\n');
		}
		else {
			search(root.lt, a);
			search(root.rt, a);
		}
	}
	
	void print(int n) { // 2 ~ n๊นŒ์ง€ ๋ถ€๋ชจ ๋…ธ๋“œ์ถœ
		for(int i = 2; i <= n; i++) {
			search(root, i);
		}
		
		System.out.println(sb);
	}
}

public class Main {		
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
				
		// ํŠธ๋ฆฌ ์ •๋ณด ์ž…๋ ฅ
		Tree tree = new Tree();
		for(int i = 0; i < n - 1; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			tree.add(tree.root, Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
		}
		
		tree.print(n);
		
		br.close();
	}
}

 

DFS, ArrayList ๋ฐฐ์—ด์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•

/* ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ - 11725 :: ํŠธ๋ฆฌ์˜ ๋ถ€๋ชจ ์ฐพ๊ธฐ - dfs */

import java.util.*;
import java.io.*;

public class Main {
	static List<Integer>[] list;
	static int[] parent;
	static boolean[] check;
	static int n;
	
	static void dfs(int l) {
		// ์ค‘๋ณต ์ฒดํฌ
		check[l] = true;
		
		// ๊ฐ€์žฅ ์ฒ˜์Œ์— ๋‚˜์˜ค๋Š” ๊ฐ’์ด ๋ฌด์กฐ๊ฑด ๋ถ€๋ชจ
		for(int i : list[l]) {
			// ์ค‘๋ณต ๊ฒ€์‚ฌ
			if(!check[i]) {
				parent[i] = l;
				dfs(i);
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws IOException{
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();

		list =  new ArrayList[n + 1];
		parent = new int[n+1];
		check = new boolean[n+1];
		
		// ArrayList ๋ฐฐ์—ด์ƒ์„ฑ
		for(int i = 1; i <= n; i++) {
			list[i]  = new ArrayList<>();
		}
		
		// ๋…ธ๋“œ ์ •๋ณด ์ž…๋ ฅ
		for(int i = 0; i < n -1; i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			
			list[a].add(b);
			list[b].add(a);
		}
		
		dfs(1);
		
		for(int i = 2; i <= n; i++) System.out.println(parent[i]);
		sc.close();
	}
}
Comments