답안 #1011116

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1011116 2024-06-29T20:14:12 Z Oz121 Burza (COCI16_burza) Java 11
160 / 160
662 ms 27348 KB
import java.io.*;
import java.util.*;
public class burza {
    public static int num; public static int k; public static HashMap<Integer, ArrayList<Integer>> tree;
    public static int[] depth; public static int[][] interval; //Interval of leaves covered by a node
    public static int count; public static HashMap<Integer, Integer> idx;
    public static void main(String[] args) throws IOException {
        BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer l1 = new StringTokenizer(scan.readLine());
        num = Integer.parseInt(l1.nextToken()); k = Integer.parseInt(l1.nextToken()); tree = new HashMap<>();

        for (int i = 0;i<num;i++) tree.put(i,new ArrayList<>());
        for (int i = 0;i<num-1;i++) {
            StringTokenizer st = new StringTokenizer(scan.readLine());
            int a = Integer.parseInt(st.nextToken())-1; int b = Integer.parseInt(st.nextToken())-1;
            tree.get(a).add(b); tree.get(b).add(a);
        }

        if (k*k>=num) System.out.println("DA");
        else System.out.println(solve());
    }

    public static String solve () {
        idx = new HashMap<>(); count = 0;
        depth = new int[num];
        Arrays.fill(depth, -1); depth[0] = 0;
        dfs1(0);

        interval = new int[num][2];
        for (int i = 0;i<num;i++) {
            interval[i][0] = Integer.MAX_VALUE; interval[i][1] = Integer.MIN_VALUE;
        }

        count = 0;
        for (int i = 0;i<num;i++) {
            if (depth[i]==k) {
                interval[i][0] = idx.get(i); interval[i][1] = idx.get(i);
                dfs2(i,i); count++;
            }
        }
        
        int[] dp = new int[1<<k];
        Arrays.fill(dp, -1); 

        for (int i = 1;i<1<<k;i++) {
            for (int j = 0;j<k;j++) {
                if ((i&(1<<j))==0) continue;

                int prev = i-(1<<j);

                for (int l = 0;l<num;l++) {
                    if (depth[l]!=j+1) continue;

                    if (interval[l][0]<=dp[prev]+1) {
                        dp[i] = Math.max(dp[i], interval[l][1]);
                    }
                }
            }
        }

        for (int i = 0;i<(1<<k);i++) {
            if (dp[i] == count - 1) return "DA";
        }
        return "NE";
    }

    public static void dfs1 (int curr) {
        for (int next : tree.get(curr)) {
            if (depth[next]==-1) {
                depth[next] = depth[curr]+1;
                if (depth[next]==k) {
                    idx.put(next, count); count++;
                }
                dfs1(next);
            }
        }
    }

    public static void dfs2 (int curr, int child) {
        for (int next : tree.get(curr)) {
            if (depth[next]<depth[curr]) { //Going up the tree
                if (idx.get(child)<interval[next][0]) interval[next][0] = idx.get(child);
                if (idx.get(child)>interval[next][1]) interval[next][1] = idx.get(child);
                dfs2(next, child);
            }
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 108 ms 24144 KB Output is correct
2 Correct 611 ms 22932 KB Output is correct
3 Correct 54 ms 22476 KB Output is correct
4 Correct 53 ms 24120 KB Output is correct
5 Correct 67 ms 23572 KB Output is correct
6 Correct 45 ms 22720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 541 ms 27248 KB Output is correct
2 Correct 628 ms 23396 KB Output is correct
3 Correct 50 ms 24436 KB Output is correct
4 Correct 52 ms 22404 KB Output is correct
5 Correct 593 ms 23328 KB Output is correct
6 Correct 71 ms 22412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 600 ms 22880 KB Output is correct
2 Correct 596 ms 23364 KB Output is correct
3 Correct 49 ms 22384 KB Output is correct
4 Correct 47 ms 22428 KB Output is correct
5 Correct 60 ms 23440 KB Output is correct
6 Correct 44 ms 22480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 204 ms 22960 KB Output is correct
2 Correct 639 ms 22816 KB Output is correct
3 Correct 51 ms 22308 KB Output is correct
4 Correct 47 ms 22852 KB Output is correct
5 Correct 61 ms 24908 KB Output is correct
6 Correct 45 ms 22476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 600 ms 22704 KB Output is correct
2 Correct 633 ms 23296 KB Output is correct
3 Correct 50 ms 22488 KB Output is correct
4 Correct 49 ms 24328 KB Output is correct
5 Correct 65 ms 23536 KB Output is correct
6 Correct 61 ms 22932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 568 ms 25472 KB Output is correct
2 Correct 597 ms 27256 KB Output is correct
3 Correct 55 ms 22100 KB Output is correct
4 Correct 52 ms 22296 KB Output is correct
5 Correct 67 ms 22588 KB Output is correct
6 Correct 67 ms 22976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 622 ms 23004 KB Output is correct
2 Correct 600 ms 23464 KB Output is correct
3 Correct 50 ms 22148 KB Output is correct
4 Correct 47 ms 22756 KB Output is correct
5 Correct 589 ms 23132 KB Output is correct
6 Correct 62 ms 23524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 158 ms 27348 KB Output is correct
2 Correct 572 ms 23648 KB Output is correct
3 Correct 53 ms 22632 KB Output is correct
4 Correct 51 ms 26672 KB Output is correct
5 Correct 68 ms 23276 KB Output is correct
6 Correct 66 ms 25080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 25232 KB Output is correct
2 Correct 662 ms 23160 KB Output is correct
3 Correct 51 ms 22580 KB Output is correct
4 Correct 51 ms 21688 KB Output is correct
5 Correct 55 ms 23112 KB Output is correct
6 Correct 72 ms 23652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 263 ms 23644 KB Output is correct
2 Correct 618 ms 22836 KB Output is correct
3 Correct 48 ms 22728 KB Output is correct
4 Correct 52 ms 22188 KB Output is correct
5 Correct 280 ms 22744 KB Output is correct
6 Correct 46 ms 22744 KB Output is correct