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

miinsun

[BAEKJOON] ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ 1967 :: ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ 1967 :: ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„

miinsun 2022. 4. 7. 16:31

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

ํŠธ๋ฆฌ(tree)๋Š” ์‚ฌ์ดํด์ด ์—†๋Š” ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์ด๋‹ค.
ํŠธ๋ฆฌ์—์„œ๋Š” ์–ด๋–ค ๋‘ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•ด๋„ ๋‘˜ ์‚ฌ์ด์— ๊ฒฝ๋กœ๊ฐ€ ํ•ญ์ƒ ํ•˜๋‚˜๋งŒ ์กด์žฌํ•˜๊ฒŒ ๋œ๋‹ค.
ํŠธ๋ฆฌ์—์„œ ์–ด๋–ค ๋‘ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•ด์„œ ์–‘์ชฝ์œผ๋กœ ์ซ™ ๋‹น๊ธธ ๋•Œ, ๊ฐ€์žฅ ๊ธธ๊ฒŒ ๋Š˜์–ด๋‚˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค.
์ด๋Ÿด ๋•Œ ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ๋…ธ๋“œ๋“ค์€ ์ด ๋‘ ๋…ธ๋“œ๋ฅผ ์ง€๋ฆ„์˜ ๋ ์ ์œผ๋กœ ํ•˜๋Š” ์› ์•ˆ์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.
์ด๋Ÿฐ ๋‘ ๋…ธ๋“œ ์‚ฌ์ด์˜ ๊ฒฝ๋กœ์˜ ๊ธธ์ด๋ฅผ ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์ด๋ผ๊ณ  ํ•œ๋‹ค.
์ •ํ™•ํžˆ ์ •์˜ํ•˜์ž๋ฉด ํŠธ๋ฆฌ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ๋กœ๋“ค ์ค‘์—์„œ ๊ฐ€์žฅ ๊ธด ๊ฒƒ์˜ ๊ธธ์ด๋ฅผ ๋งํ•œ๋‹ค.

์ž…๋ ฅ์œผ๋กœ ๋ฃจํŠธ๊ฐ€ ์žˆ๋Š” ํŠธ๋ฆฌ๋ฅผ ๊ฐ€์ค‘์น˜๊ฐ€ ์žˆ๋Š” ๊ฐ„์„ ๋“ค๋กœ ์ค„ ๋•Œ, ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์„ ๊ตฌํ•ด์„œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.
์•„๋ž˜์™€ ๊ฐ™์€ ํŠธ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค๋ฉด ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์€ 45๊ฐ€ ๋œ๋‹ค.

ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ๋Š” 1๋ถ€ํ„ฐ n๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๋‹ค.

 

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

์ž…๋ ฅ 

  • ํŒŒ์ผ์˜ ์ฒซ ๋ฒˆ์งธ ์ค„์€ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ n(1 ≤ n ≤ 10,000)์ด๋‹ค.
  • ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ n-1๊ฐœ์˜ ์ค„์— ๊ฐ ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋“ค์–ด์˜จ๋‹ค.
  • ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋Š” ์„ธ ๊ฐœ์˜ ์ •์ˆ˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.
  • ์ฒซ ๋ฒˆ์งธ ์ •์ˆ˜๋Š” ๊ฐ„์„ ์ด ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ๋…ธ๋“œ ์ค‘ ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ , ๋‘ ๋ฒˆ์งธ ์ •์ˆ˜๋Š” ์ž์‹ ๋…ธ๋“œ๋ฅผ, ์„ธ ๋ฒˆ์งธ ์ •์ˆ˜๋Š” ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  • ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋Š” ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ์ด ๋จผ์ € ์ž…๋ ฅ๋˜๊ณ , ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™์œผ๋ฉด ์ž์‹ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ์ด ๋จผ์ € ์ž…๋ ฅ๋œ๋‹ค. ๋ฃจํŠธ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๋Š” ํ•ญ์ƒ 1์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉฐ, ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋Š” 100๋ณด๋‹ค ํฌ์ง€ ์•Š์€ ์–‘์˜ ์ •์ˆ˜์ด๋‹ค.

 

์ถœ๋ ฅ

  • ์ฒซ์งธ ์ค„์— ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

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

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

 

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

45

 

โ€‹

๐Ÿ’ป  Main.java

  • ๋จผ์ € 2์ฐจ์› ๋ฐฐ์—ด๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์„๋•Œ, ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ ์˜ค๋ฅ˜๊ฐ€ ๋–ณ๋‹ค
  • ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์œผ๋กœ ArrayList๋กœ ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • ์ผ๋‹จ ํŠน์ • ๋…ธ๋“œ(1๋ฒˆ ๋…ธ๋“œ)์—์„œ ๊ฐ€์žฅ ๋จผ ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค.
  • ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ๋–จ์–ด์ง„ ๋…ธ๋“œ์—์„œ ๊ฐ€์žฅ ํฐ ์ง€๋ฆ„์„ ์ฐพ์•„์ค€๋‹ค.
/* ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ - 1967 :: ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ */

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

class Node{
	int node, dist;
	public Node(int node, int dist) {
		this.node = node;
		this.dist = dist;
	}
}

public class Main {	
	static ArrayList<Node>[] list;
	static int n;
	static int max = 0;
	static int total = 0;
	static int leaf;
	
	static void dfs(int node, int parent, int total) {
		if(total > max) {
			max = total;
			leaf = node;
		}
		
		for(Node tmp : list[node]) {
			// ์ผ๋Œ€์ผ ์—ฐ๊ฒฐ์ผ๋•Œ, ๋ถ€๋ชจ๊ฐ’์€ ๋ฌด์‹œ
			if(tmp.node == parent) continue;
			dfs(tmp.node, node, tmp.dist + total);
		}
	}
	
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws IOException{
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		
		list = new ArrayList[n + 1];
		for(int i = 0; i <= n; i++)
			list[i] = new ArrayList<>();
		
		for(int i = 0; i < n - 1; i++) {
			int s = sc.nextInt();
			int e = sc.nextInt();
			int len = sc.nextInt();
			
			list[s].add(new Node(e, len));
			list[e].add(new Node(s, len));
		}
		
		// ํŠน์ • ์ •์ ์—์„œ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ์žˆ๋Š” ๋…ธ๋“œ์˜ ์ง€๋ฆ„๊ณผ ์ •๋ณด ์–ป๊ธฐ
		dfs(1, 0, 0);
		// ๋ฆฌํ”„๋…ธ๋“œ์—์„œ ์˜ฌ๋ผ์˜ค๋ฉด์„œ ๊ฐ€์žฅ ๊ธด ์ง€๋ฆ„ ์ฐพ๊ธฐ
		dfs(leaf, 0, 0);
		
		System.out.println(max);
		sc.close();
	}
}
Comments