๋ฌธ์ œ ์„ค๋ช…


๐Ÿ’ก Info

๋‚ด์šฉ

N๊ฐœ์˜ ๋งˆ์„๋กœ ์ด๋ฃจ์–ด์ง„ ๋‚˜๋ผ๊ฐ€ ์žˆ๋‹ค. ํŽธ์˜์ƒ ๋งˆ์„์—๋Š” 1๋ถ€ํ„ฐ N๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™์–ด ์žˆ๋‹ค๊ณ  ํ•˜์ž. ์ด ๋‚˜๋ผ๋Š” ํŠธ๋ฆฌ(Tree) ๊ตฌ์กฐ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ์ฆ‰ ๋งˆ์„๊ณผ ๋งˆ์„ ์‚ฌ์ด๋ฅผ ์ง์ ‘ ์ž‡๋Š” N-1๊ฐœ์˜ ๊ธธ์ด ์žˆ์œผ๋ฉฐ, ๊ฐ ๊ธธ์€ ๋ฐฉํ–ฅ์„ฑ์ด ์—†์–ด์„œ A๋ฒˆ ๋งˆ์„์—์„œ B๋ฒˆ ๋งˆ์„๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋ฉด B๋ฒˆ ๋งˆ์„์—์„œ A๋ฒˆ ๋งˆ์„๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ๋˜, ๋ชจ๋“  ๋งˆ์„์€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค. ๋‘ ๋งˆ์„ ์‚ฌ์ด์— ์ง์ ‘ ์ž‡๋Š” ๊ธธ์ด ์žˆ์„ ๋•Œ, ๋‘ ๋งˆ์„์ด ์ธ์ ‘ํ•ด ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

์ด ๋‚˜๋ผ์˜ ์ฃผ๋ฏผ๋“ค์—๊ฒŒ ์„ฑ์ทจ๊ฐ์„ ๋†’์—ฌ ์ฃผ๊ธฐ ์œ„ํ•ด, ๋‹ค์Œ ์„ธ ๊ฐ€์ง€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด์„œ N๊ฐœ์˜ ๋งˆ์„ ์ค‘ ๋ช‡ ๊ฐœ์˜ ๋งˆ์„์„ '์šฐ์ˆ˜ ๋งˆ์„'๋กœ ์„ ์ •ํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

1๏ธโƒฃ '์šฐ์ˆ˜ ๋งˆ์„'๋กœ ์„ ์ •๋œ ๋งˆ์„ ์ฃผ๋ฏผ ์ˆ˜์˜ ์ด ํ•ฉ์„ ์ตœ๋Œ€๋กœ ํ•ด์•ผ ํ•œ๋‹ค.
2๏ธโƒฃ ๋งˆ์„ ์‚ฌ์ด์˜ ์ถฉ๋Œ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ, ๋งŒ์ผ ๋‘ ๋งˆ์„์ด ์ธ์ ‘ํ•ด ์žˆ์œผ๋ฉด ๋‘ ๋งˆ์„์„ ๋ชจ๋‘ '์šฐ์ˆ˜ ๋งˆ์„'๋กœ ์„ ์ •ํ•  ์ˆ˜๋Š” ์—†๋‹ค. ์ฆ‰ '์šฐ์ˆ˜ ๋งˆ์„'๋ผ๋ฆฌ๋Š” ์„œ๋กœ ์ธ์ ‘ํ•ด ์žˆ์„ ์ˆ˜ ์—†๋‹ค.
3๏ธโƒฃ ์„ ์ •๋˜์ง€ ๋ชปํ•œ ๋งˆ์„์— ๊ฒฝ๊ฐ์‹ฌ์„ ๋ถˆ๋Ÿฌ์ผ์œผํ‚ค๊ธฐ ์œ„ํ•ด์„œ, '์šฐ์ˆ˜ ๋งˆ์„'๋กœ ์„ ์ •๋˜์ง€ ๋ชปํ•œ ๋งˆ์„์€ ์ ์–ด๋„ ํ•˜๋‚˜์˜ '์šฐ์ˆ˜ ๋งˆ์„'๊ณผ๋Š” ์ธ์ ‘ํ•ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

๊ฐ ๋งˆ์„ ์ฃผ๋ฏผ ์ˆ˜์™€ ๋งˆ์„ ์‚ฌ์ด์˜ ๊ธธ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋„๋ก '์šฐ์ˆ˜ ๋งˆ์„'์„ ์„ ์ •ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๐Ÿ“ฅ์ž…๋ ฅ ์กฐ๊ฑด

  • ์ฒซ์งธ ์ค„์— ์ •์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค N โ‰ค 10,000)
  • ๋‘˜์งธ ์ค„์—๋Š” ๋งˆ์„ ์ฃผ๋ฏผ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” N๊ฐœ์˜ ์ž์—ฐ์ˆ˜๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค.
  • 1๋ฒˆ ๋งˆ์„๋ถ€ํ„ฐ N๋ฒˆ ๋งˆ์„๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง€๋ฉฐ, ์ฃผ๋ฏผ ์ˆ˜๋Š” 10,000 ์ดํ•˜์ด๋‹ค.
  • ์…‹์งธ ์ค„๋ถ€ํ„ฐ N-1๊ฐœ ์ค„์— ๊ฑธ์ณ์„œ ์ธ์ ‘ํ•œ ๋‘ ๋งˆ์„์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค.
7
1000 3000 4000 1000 2000 2000 7000
1 2
2 3
4 3
4 5
6 2
6 7

๐Ÿ“ค์ถœ๋ ฅ ์กฐ๊ฑด

  • ์ฒซ์งธ ์ค„์— '์šฐ์ˆ˜ ๋งˆ์„'์˜ ์ฃผ๋ฏผ ์ˆ˜์˜ ์ด ํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.
14000


๋ฌธ์ œ ์ดํ•ด


  • ํŠธ๋ฆฌ ๊ตฌ์กฐ๋กœ ์ด๋ค„์ง„ ๋งˆ์„ ์ค‘ ์šฐ์ˆ˜ ๋งˆ์„๋กœ ์„ ์ •๋  ์ˆ˜ ์žˆ๋Š” ๋งˆ์„์˜ ์ฃผ๋ฏผ ์ˆ˜ ์ด ํ•ฉ์„ ๊ตฌํ•˜๊ธฐ


์•Œ๊ณ ๋ฆฌ์ฆ˜


์‹ค์ œ ํ’€์ด ์‹œ๊ฐ„ : 35๋ถ„

  • ์ž…๋ ฅ

    • ๋งˆ์„ ์ˆ˜ N ์ž…๋ ฅ๋ฐ›๊ธฐ
    • ๋งˆ์„ ์ฃผ๋ฏผ ์ˆ˜ arr ์ž…๋ ฅ๋ฐ›๊ธฐ
    • ์ธ์ ‘ ์ˆ˜ adj ์ž…๋ ฅ ๋ฐ›๊ธฐ
  • ๊ณ„์‚ฐ

    • DFS + ๋ฐ”ํ…€์—… + ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ด์šฉ
    • ์•„๋ž˜์˜ ๋งˆ์„๋ถ€ํ„ฐ ๋น„๊ตํ•ด์„œ Math.max๋กœ ์ตœ๋Œ€๊ฐ’์„ ๊ณ„์† ๋Œ€์น˜ํ•ด ๊ฐฑ์‹ ํ•˜๊ธฐ
  • ์ถœ๋ ฅ

    • ์ตœ์ข… ๊ฐ’ result ์ถœ๋ ฅํ•˜๊ธฐ
  • ๋ณ„๋„ ๋ฉ”์„œ๋“œ

    • DFS ๊ณ„์‚ฐ ๋ฉ”์„œ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ
import java.util.*;

public class Main {

    static int N;
    static int[] arr;
    static int[][] adj;
    static int[][] town;
    static boolean[] visited;

    static int result;

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        N = sc.nextInt();

        arr = new int[N+1];
        for(int i=1; i<=N; i++){
            arr[i] = sc.nextInt();
        }

        adj = new int[N+1][N+1];
        for(int i=0; i<N+1; i++){
            for(int j=0; j<N+1; j++) {
                int u = sc.nextInt();
                int v = sc.nextInt();
                adj[u][v] = 1;
                adj[v][u] = 1;

            }
        }

        town = new int[N+1][2];
        visited = new boolean[N+1];

        dfs(1);

        result = Math.max(town[1][0] , town[1][1]);

        System.out.println(result);
    }

    public static void dfs(int n){
        visited[n] = true;
        town[n][0] = 0;
        town[n][1] = arr[n];

        for (int i = 1; i <= N; i++) {
            if (adj[n][i] == 1 && !visited[i]) {
                dfs(i);
                town[n][0] += Math.max(town[i][0], town[i][1]);
                town[n][1] += town[i][0];
            }
        }

    }
}


์˜ค๋‹ต์ฒดํฌ


  • ์ž…๋ ฅ๋ฐ›์€ ๋ฐฐ์—ด๋“ค์˜ ๋ฒ”์œ„๊ฐ€ ์ž˜๋ชป๋˜์–ด ์ถœ๋ ฅ์ด ์•ˆ๋˜๋Š” ๋ฌธ์ œ ๋ฐœ์ƒ
    • arr : 1~N๊นŒ์ง€ ํฌํ•จ๋˜๋„๋ก ๋ณ€๊ฒฝ + i,j๊ฐ€ ์•„๋‹Œ i๋กœ ๋ณ€๊ฒฝ
    • town : ๋ฒ”์œ„๋ฅผ [N+1][N+1] ์—์„œ [N+1][2]๋กœ ๋ณ€๊ฒฝํ•ด DP๋ฅผ ๊ตฌํ˜„
      • "์šฐ์ˆ˜ ๋งˆ์„์ด ์•„๋‹Œ ๊ฒฝ์šฐ ์ตœ๋Œ€ ์ฃผ๋ฏผ ์ˆ˜"์™€ "์šฐ์ˆ˜ ๋งˆ์„์ธ ๊ฒฝ์šฐ ์ตœ๋Œ€ ์ฃผ๋ฏผ ์ˆ˜"๋กœ ๋‚˜๋ˆ„๊ธฐ ์œ„ํ•จ.
//before
arr = new int[N+1];
for(int i=1; i<=N; i++){
	arr[i] = sc.nextInt();
}
adj = new int[N+1][N+1];
for(int i=0; i<N+1; i++){
	for(int j=0; j<N+1; j++) {
    	int u = sc.nextInt();
        int v = sc.nextInt();
        adj[u][v] = 1;
        adj[v][u] = 1;

	}
}

town = new int[N+1][2];
//after
arr = new int[N+1];
for(int i=1; i<=N; i++){
	arr[i] = sc.nextInt();
}

adj = new int[N+1][N+1];
for(int i=0; i<N-1; i++){
	int u = sc.nextInt();
    int v = sc.nextInt();
    adj[u][v] = 1;
    adj[v][u] = 1;
}

town = new int[N+1][2];

  • ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ ๋ฐœ์ƒ
    • adj๋ฅผ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€๊ฒฝํ•ด ํ•ด๊ฒฐ
...
static List<Integer>[] adj;

...
adj = new ArrayList[N+1];
for(int i = 1; i <= N; i++){
	adj[i] = new ArrayList<>();
}

for(int i = 0; i < N-1; i++){
	int u = sc.nextInt();
    int v = sc.nextInt();
    adj[u].add(v);
    adj[v].add(u);
}

...

for (int i = 0; i < adj[n].size(); i++) {
	int neighbor = adj[n].get(i);
    if (!visited[neighbor]) {
    	dfs(neighbor);
        town[n][0] += Math.max(town[neighbor][0], town[neighbor][1]);
        town[n][1] += town[neighbor][0];
            }
        }
    }
}


์ตœ์ข… ํ’€์ด


์‹ค์ œ ํ’€์ด ์‹œ๊ฐ„ : 55๋ถ„(์ฒซ ํ’€์ด ์‹œ๊ฐ„ ํฌํ•จ)

  • ์ž…๋ ฅ

    • ๋งˆ์„ ์ˆ˜ N ์ž…๋ ฅ๋ฐ›๊ธฐ
    • ๋งˆ์„ ์ฃผ๋ฏผ ์ˆ˜ arr ์ž…๋ ฅ๋ฐ›๊ธฐ
    • ์ธ์ ‘ ์ˆ˜ adj ์ž…๋ ฅ ๋ฐ›๊ธฐ
  • ๊ณ„์‚ฐ

    • ์šฐ์ˆ˜ ๋งˆ์„ town, ๋ฐฉ๋ฌธ ์ •๋ณด visited ์„ ์–ธ
    • ๊ฒฐ๊ณผ result ๋ณ€์ˆ˜ ์„ ์–ธ
    • DFS + ๋ฐ”ํ…€์—… + ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ด์šฉ
    • ์•„๋ž˜์˜ ๋งˆ์„๋ถ€ํ„ฐ ๋น„๊ตํ•ด์„œ Math.max๋กœ ์ตœ๋Œ€๊ฐ’์„ ๊ณ„์† ๋Œ€์น˜ํ•ด ๊ฐฑ์‹ ํ•˜๊ธฐ
  • ์ถœ๋ ฅ

    • ์ตœ์ข… ๊ฐ’ result ์ถœ๋ ฅํ•˜๊ธฐ
  • ๋ณ„๋„ ๋ฉ”์„œ๋“œ

    • DFS ๊ณ„์‚ฐ ๋ฉ”์„œ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ
import java.util.*;

public class Main {

    static int N;
    static int[] arr;
    static List<Integer>[] adj;
    static int[][] town;
    static boolean[] visited;

    static int result;

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        N = sc.nextInt();

        arr = new int[N+1];
        for(int i=1; i<=N; i++){
            arr[i] = sc.nextInt();
        }
        
        adj = new ArrayList[N+1];
        for(int i = 1; i <= N; i++){
            adj[i] = new ArrayList<>();
        }

        for(int i = 0; i < N-1; i++){
            int u = sc.nextInt();
            int v = sc.nextInt();
            adj[u].add(v);
            adj[v].add(u);
        }

        town = new int[N+1][2];
        visited = new boolean[N+1];

        dfs(1);

        result = Math.max(town[1][0] , town[1][1]);

        System.out.println(result);
    }

    public static void dfs(int n){
        visited[n] = true;
        town[n][0] = 0;
        town[n][1] = arr[n];

        for (int i = 0; i < adj[n].size(); i++) {
            int neighbor = adj[n].get(i);
            if (!visited[neighbor]) {
                dfs(neighbor);
                town[n][0] += Math.max(town[neighbor][0], town[neighbor][1]);
                town[n][1] += town[neighbor][0];
            }
        }
    }
}

profile
์–ธ์  ๊ฐ€ ๋‚ด ์ฝ”๋“œ๋กœ ์„ธ์ƒ์— ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ๋„๋ก, BE&Data Science ๊ฐœ๋ฐœ ๊ธฐ๋ก ๋…ธํŠธโ˜˜๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€