Submission #1011091

# Submission time Handle Problem Language Result Execution time Memory
1011091 2024-06-29T19:43:11 Z Oz121 Burza (COCI16_burza) Java 11
0 / 160
477 ms 26820 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("NE");
        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++;
            }
        }


        /*for (int i = 0;i<num;i++) {
            System.out.println(interval[i][0]+" "+interval[i][1]);
        }*/

        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]);
                    }
                }
            }
        }

        if (dp[(1<<k)-1]==count-1) return "DA";
        else 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);
                else if (idx.get(child)>interval[next][1]) interval[next][1] = idx.get(child);
                dfs2(next, child);
            }
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 91 ms 25440 KB Output is correct
2 Correct 466 ms 24788 KB Output is correct
3 Incorrect 49 ms 22436 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 427 ms 22760 KB Output is correct
2 Correct 444 ms 22916 KB Output is correct
3 Incorrect 51 ms 21908 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 450 ms 22588 KB Output is correct
2 Correct 451 ms 23000 KB Output is correct
3 Incorrect 56 ms 24272 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 158 ms 22444 KB Output is correct
2 Correct 466 ms 22456 KB Output is correct
3 Incorrect 58 ms 22324 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 446 ms 22696 KB Output is correct
2 Correct 436 ms 26820 KB Output is correct
3 Incorrect 52 ms 21864 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 440 ms 22832 KB Output is correct
2 Correct 452 ms 25024 KB Output is correct
3 Incorrect 48 ms 21900 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 443 ms 23048 KB Output is correct
2 Correct 461 ms 22568 KB Output is correct
3 Incorrect 50 ms 24720 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 132 ms 22884 KB Output is correct
2 Correct 470 ms 23032 KB Output is correct
3 Incorrect 55 ms 22392 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 92 ms 22496 KB Output is correct
2 Correct 477 ms 22272 KB Output is correct
3 Incorrect 55 ms 22972 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 243 ms 24552 KB Output is correct
2 Correct 475 ms 24992 KB Output is correct
3 Incorrect 57 ms 22376 KB Output isn't correct
4 Halted 0 ms 0 KB -