Notice
Recent Posts
Recent Comments
Today
Total
01-10 18:14
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[๋ฐฑ์ค€ 15681] ํŠธ๋ฆฌ์™€ ์ฟผ๋ฆฌ (ํŠธ๋ฆฌ DP) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[๋ฐฑ์ค€ 15681] ํŠธ๋ฆฌ์™€ ์ฟผ๋ฆฌ (ํŠธ๋ฆฌ DP)

WonderJay 2023. 6. 7. 12:10
728x90
๋ฐ˜์‘ํ˜•
SMALL

15681๋ฒˆ: ํŠธ๋ฆฌ์™€ ์ฟผ๋ฆฌ (acmicpc.net)

 

15681๋ฒˆ: ํŠธ๋ฆฌ์™€ ์ฟผ๋ฆฌ

ํŠธ๋ฆฌ์˜ ์ •์ ์˜ ์ˆ˜ N๊ณผ ๋ฃจํŠธ์˜ ๋ฒˆํ˜ธ R, ์ฟผ๋ฆฌ์˜ ์ˆ˜ Q๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 105, 1 ≤ R ≤ N, 1 ≤ Q ≤ 105) ์ด์–ด N-1์ค„์— ๊ฑธ์ณ, U V์˜ ํ˜•ํƒœ๋กœ ํŠธ๋ฆฌ์— ์†ํ•œ ๊ฐ„์„ ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ U, V ≤ N, U ≠ V)

www.acmicpc.net

 

ํŠธ๋ฆฌ์—์„œ DP ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ž…๋ฌธ ๋ฌธ์ œ!

 

๋ฌธ์ œ๋Š” ๋‹จ์ˆœํ•˜๋‹ค.

 

Q ๊ฐœ์˜ ์ฟผ๋ฆฌ๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ์ด์— ๋”ฐ๋ฅธ ์ถœ๋ ฅ์„ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

์ฟผ๋ฆฌ๋Š” ๋…ธ๋“œ V ์— ๋Œ€ํ•œ ์„œ๋ธŒ ํŠธ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋…ธ๋“œ๋“ค์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

์ž…๋ ฅ ์กฐ๊ฑด์„ ๋ณด๋ฉด,

 

ํŠธ๋ฆฌ์˜ ํฌ๊ธฐ๋„ ์ƒ๋‹นํžˆ ํฐ ํŽธ์ด๊ธฐ๋„ ํ•˜์ง€๋งŒ,

 

์ฟผ๋ฆฌ๊ฐ€ ์ตœ๋Œ€ 100,000 ๊ฐœ๊ฐ€ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

 

๋…ธ๋“œ V ์— ๋Œ€ํ•œ ์„œ๋ธŒ ํŠธ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋…ธ๋“œ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŒ…ํ•˜๋Š” ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์ธ

 

V ์— ๋Œ€ํ•œ dfs ํƒ์ƒ‰์„ ์ฟผ๋ฆฌ๋งˆ๋‹ค ์ˆ˜ํ–‰ํ•œ๋‹ค๋ฉด

 

์‚ฌ์‹ค, ๊ณ„์‚ฐํ•ด๋ณด์ง€ ์•Š์•„๋„ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜๋ณต๋œ ์—ฐ์‚ฐ์„ ๋ฉ”๋ชจ์ด์ œ์ด์…˜ํ•˜์—ฌ dp ํ…Œ์ด๋ธ”์— ์ €์žฅํ•˜๊ณ ,

 

์ฟผ๋ฆฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๊ทธ๋ƒฅ dp ์—์„œ ๊บผ๋‚ด์„œ ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜์ž.

 

์•„๋ž˜์™€ ๊ฐ™์€ ํŠธ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค๋ฉด,

 

์ถœ์ฒ˜: geeks for geeks

1 ๋ฒˆ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์„œ๋ธŒ ๋…ธ๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์ •์ ์€ ๊ฐœ์ˆ˜๋Š” ์ž๊ธฐ ์ž์‹ ์„ ํฌํ•จํ•œ 7

2 ๋ฒˆ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์„œ๋ธŒ ๋…ธ๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์ •์ ์€ ๊ฐœ์ˆ˜๋Š” ์ž๊ธฐ ์ž์‹ ์„ ํฌํ•จํ•œ 3

3 ๋ฒˆ ๋…ธ๋“œ์— ๋Œ€ํ•œ  ์„œ๋ธŒ ๋…ธ๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์ •์ ์€ ๊ฐœ์ˆ˜๋Š” ์ž๊ธฐ ์ž์‹ ์„ ํฌํ•จํ•œ 3

4,5,6,7 ๋ฒˆ ๋…ธ๋“œ์— ๋Œ€ํ•œ  ์„œ๋ธŒ ๋…ธ๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์ •์ ์€ ๊ฐœ์ˆ˜๋Š” ์ž๊ธฐ ์ž์‹ ์„ ํฌํ•จํ•œ 1

 

์ƒ๊ฐํ•ด๋ณด๋ฉด

 

1๋ฒˆ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์„œ๋ธŒ ๋…ธ๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์ •์ ์˜ ๊ฐœ์ˆ˜๋Š”

  =  2 ๋ฒˆ ๋…ธ๋“œ์— ๋Œ€ํ•œ  ์„œ๋ธŒ ๋…ธ๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์ •์ ์€ ๊ฐœ์ˆ˜ + 3 ๋ฒˆ ๋…ธ๋“œ์— ๋Œ€ํ•œ  ์„œ๋ธŒ ๋…ธ๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์ •์ ์€ ๊ฐœ์ˆ˜

์ด๋‹ค.

 

2 ๋ฒˆ ๋…ธ๋“œ์— ๋Œ€ํ•œ  ์„œ๋ธŒ ๋…ธ๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์ •์ ์€ ๊ฐœ์ˆ˜๋Š”

  =  4 ๋ฒˆ ๋…ธ๋“œ์— ๋Œ€ํ•œ  ์„œ๋ธŒ ๋…ธ๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์ •์ ์€ ๊ฐœ์ˆ˜ + 5 ๋ฒˆ ๋…ธ๋“œ์— ๋Œ€ํ•œ  ์„œ๋ธŒ ๋…ธ๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์ •์ ์€ ๊ฐœ์ˆ˜์ด๋‹ค.

 

์ด์™€ ๊ฐ™์ด ํฐ ๋ฌธ์ œ๊ฐ€ ์ž‘์€ ๋ฌธ์ œ๋กœ ์ชผ๊ฐœ์ง€๋Š” ํŠน์„ฑ์„ ์ด์šฉํ•˜์—ฌ ํŠธ๋ฆฌ์— ๋Œ€ํ•œ ์„œ๋ธŒ ๋…ธ๋“œ ๊ฐœ์ˆ˜๋ฅผ ๋ชจ๋‘ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

dp ํ…Œ์ด๋ธ”์„ 0 ์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ ,

 

root ๋…ธ๋“œ์— ๋ฐฉ๋ฌธํ•œ๋‹ค. (์—ฌ๊ธฐ์„œ๋Š” 1)

 

๊ทธ๋ฆฌ๊ณ  ์„œ๋ธŒ ๋…ธ๋“œ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŒ…ํ•  ๋ณ€์ˆ˜์ธ ret ์„ 1 ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  dp[1] ์„ ํ™•์ธํ–ˆ์„ ๋•Œ 0 ์ด๋ผ๋ฉด, ๋” ์ž‘์€ ๋ฌธ์ œ๋ฅผ ํ™•์ธํ•œ๋‹ค.(์—ฐ๊ฒฐ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰)

 

1๊ณผ ์—ฐ๊ฒฐ๋œ 2, 3 ๋…ธ๋“œ์— ๋Œ€ํ•˜์—ฌ dp[2] , dp[3] ์ด 0 ์ด๋ผ๋ฉด 2 ๋ฒˆ, 3 ๋ฒˆ ๋…ธ๋“œ์— ๋Œ€ํ•ด์„œ ๋” ์ž‘์€ ๋ฌธ์ œ๋ฅผ ํ™•์ธํ•˜๊ณ 

 

ret ์— ๊ทธ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ˆ„์ ์‹œํ‚จ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ dp[ํ˜„์žฌ ํƒ์ƒ‰ํ•˜๊ณ  ์žˆ๋Š” ๋…ธ๋“œ] ์— ret ์„ ์ฐจ๋ก€๋Œ€๋กœ ์ €์žฅํ•˜๋‹ค๋ณด๋ฉด ๊ฒฐ๊ตญ ์ „์ฒด ํŠธ๋ฆฌ์˜ dp ํ…Œ์ด๋ธ”์ด ์ฑ„์›Œ์ง€๋Š” ๊ตฌ์กฐ์ด๋‹ค.

 

์‚ฌ์‹ค ๋ง๋กœ ์„ค๋ช…ํ•˜๋ฉด ์ž˜ ์™€๋‹ฟ์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™๋‹ค..

 

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์ดํ•ดํ•˜๋Š” ๊ฒŒ ๋” ์‰ฌ์šธ ๊ฒƒ์ด๋‹ค.

 

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

public class Main {

    /*
    ํŠธ๋ฆฌ์˜ ์ •์ ์˜ ์ˆ˜ N๊ณผ ๋ฃจํŠธ์˜ ๋ฒˆํ˜ธ R, ์ฟผ๋ฆฌ์˜ ์ˆ˜ Q๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 105, 1 ≤ R ≤ N, 1 ≤ Q ≤ 105)
    ์ด์–ด N-1์ค„์— ๊ฑธ์ณ, U V์˜ ํ˜•ํƒœ๋กœ ํŠธ๋ฆฌ์— ์†ํ•œ ๊ฐ„์„ ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ U, V ≤ N, U ≠ V)
    ์ด๋Š” U์™€ V๋ฅผ ์–‘ ๋์ ์œผ๋กœ ํ•˜๋Š” ๊ฐ„์„ ์ด ํŠธ๋ฆฌ์— ์†ํ•จ์„ ์˜๋ฏธํ•œ๋‹ค.
    ์ด์–ด Q์ค„์— ๊ฑธ์ณ, ๋ฌธ์ œ์— ์„ค๋ช…ํ•œ U๊ฐ€ ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ U ≤ N)
    ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ํŠธ๋ฆฌ๋Š” ํ•ญ์ƒ ์˜ฌ๋ฐ”๋ฅธ ํŠธ๋ฆฌ์ž„์ด ๋ณด์žฅ๋œ๋‹ค.
     */
    static int n;
    static int r;
    static int q;
    static ArrayList<Integer>[] tree;
    static boolean[] visited;
    static int[] dp;

    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer stk;
        stk = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(stk.nextToken());
        int r = Integer.parseInt(stk.nextToken());
        int q = Integer.parseInt(stk.nextToken());

        tree = new ArrayList[n + 1];
        for (int i = 0; i <= n; i++) {
            tree[i] = new ArrayList<>();
        }
        for (int i = 0; i < n - 1; i++) {
            stk = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(stk.nextToken());
            int b = Integer.parseInt(stk.nextToken());
            tree[a].add(b);
            tree[b].add(a);
        }
        dp = new int[n + 1];
        visited = new boolean[n + 1];

        find(r);

        while (q-- > 0) {
            int t = Integer.parseInt(br.readLine());
            bw.write(dp[t] + "\n");
        }
        bw.flush();
        bw.close();
    }

    static int find(int v) {
        visited[v] = true;
        int ret = 1;
        if (dp[v] == 0) {
            for (int iter : tree[v]) {
                if (!visited[iter]) {
                    ret += find(iter);
                    visited[iter] = true;
                }
            }
            dp[v] = ret;
        }
        return dp[v];
    }
}
728x90
๋ฐ˜์‘ํ˜•
LIST
Comments