Submission #1044557


Source Code Expand

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskF solver = new TaskF();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskF {
        static final int INF = (int) 1e6;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            TaskF.Vertex[] vs = new TaskF.Vertex[n];
            for (int i = 0; i < n; ++i) {
                vs[i] = new TaskF.Vertex(i + 1);
            }
            for (int i = 0; i < n - 1; ++i) {
                TaskF.Vertex a = vs[in.nextInt() - 1];
                TaskF.Vertex b = vs[in.nextInt() - 1];
                a.adj.add(b);
                b.adj.add(a);
            }
            String likes = in.next();
            for (int i = 0; i < n; ++i) {
                vs[i].liked = likes.charAt(i) == '1';
            }
            vs[0].dfs1(null);
            long res = 1;
            for (TaskF.Vertex a : vs) {
                a.getMaxDepth(null);
            }
            for (TaskF.Vertex a : vs)
                if (a.liked) {
                    ++res;
                    res += Math.max(0, a.max2 - 1);
                }
            for (TaskF.Vertex a : vs)
                if (a.adj.size() >= 2) {
                    for (TaskF.Vertex b : a.adj) {
                        if (b.getMinPropagate(a) <= 0) {
                            ++res;
                        }
                    }
                    int minSimple = a.max3;
                    int minSimpleUpTo2 = a.max2;
                    for (TaskF.Vertex b : a.adj) {
                        int d = Math.max(1, b.getMinPropagate(a));
                        int dd = b.getMaxDepth(a) + 1;
                        minSimple = Math.min(minSimple, Math.max(dd, d));
                        if (b != a.maxVia && b != a.max2Via) {
                            minSimpleUpTo2 = Math.min(minSimpleUpTo2, Math.max(dd, d));
                        }
                        int maxd;
                        if (b == a.maxVia || b == a.max2Via) {
                            maxd = a.max3 - 1;
                        } else {
                            maxd = a.max2 - 1;
                        }
                        res += Math.max(0, Math.min(dd - 1, maxd) - d + 1);
                    }
                    if (!a.liked) {
                        res += Math.max(0, a.max3 - minSimple);
                        res += Math.max(0, a.max2 - Math.max(a.max3, minSimpleUpTo2));
                    }
                }
            out.println(res);
        }

        static class Vertex {
            int index;
            boolean liked;
            int subtreeLiked;
            TaskF.Vertex parent;
            List<TaskF.Vertex> adj = new ArrayList<>();
            int state = 0;
            TaskF.Vertex skipped = null;
            int max = 0;
            TaskF.Vertex maxVia = null;
            int max2 = 0;
            TaskF.Vertex max2Via = null;
            int max3 = 0;
            TaskF.Vertex max3Via = null;
            int min1p = INF;
            TaskF.Vertex min1pVia = null;
            int min2p = INF;
            TaskF.Vertex min2pVia = null;
            int minFromMax = INF;
            int minFromMax2 = INF;
            int state2 = 0;
            TaskF.Vertex skipped2 = null;

            public Vertex(int index) {
                this.index = index;
            }

            public int getMaxDepth(TaskF.Vertex except) {
                switch (state) {
                    case 0:
                        for (TaskF.Vertex v : adj)
                            if (v != except) {
                                int cur = 1 + v.getMaxDepth(this);
                                if (cur > max) {
                                    max3 = max2;
                                    max3Via = max2Via;
                                    max2 = max;
                                    max2Via = maxVia;
                                    max = cur;
                                    maxVia = v;
                                } else if (cur > max2) {
                                    max3 = max2;
                                    max3Via = max2Via;
                                    max2 = cur;
                                    max2Via = v;
                                } else if (cur > max3) {
                                    max3 = cur;
                                    max3Via = v;
                                }
                            }
                        skipped = except;
                        state = except == null ? 2 : 1;
                        return max;

                    case 1:
                        if (skipped == except) {
                            return max;
                        }
                    {
                        TaskF.Vertex v = skipped;
                        int cur = 1 + v.getMaxDepth(this);
                        if (cur > max) {
                            max3 = max2;
                            max3Via = max2Via;
                            max2 = max;
                            max2Via = maxVia;
                            max = cur;
                            maxVia = v;
                        } else if (cur > max2) {
                            max3 = max2;
                            max3Via = max2Via;
                            max2 = cur;
                            max2Via = v;
                        } else if (cur > max3) {
                            max3 = cur;
                            max3Via = v;
                        }
                        skipped = null;
                        state = 2;
                    }
                    // FALLSTHROUGH

                    case 2:
                        if (except == maxVia) {
                            return max2;
                        } else {
                            return max;
                        }

                    default:
                        throw new RuntimeException();
                }
            }

            public void dfs1(TaskF.Vertex parent) {
                this.parent = parent;
                subtreeLiked = liked ? 1 : 0;
                for (TaskF.Vertex v : adj)
                    if (v != parent) {
                        v.dfs1(this);
                        subtreeLiked += v.subtreeLiked;
                    }
            }

            public int getMinPropagate(TaskF.Vertex dest) {
                int res;
                switch (state2) {
                    case 0:
                        for (TaskF.Vertex child : adj)
                            if (child != dest) {
                                int c = child.getMinPropagate(this);
                                if (child == maxVia) {
                                    minFromMax = c;
                                } else if (child == max2Via) {
                                    minFromMax2 = c;
                                } else if (c < min1p) {
                                    min2p = min1p;
                                    min2pVia = min1pVia;
                                    min1p = c;
                                    min1pVia = child;
                                } else if (c < min2p) {
                                    min2p = c;
                                    min2pVia = child;
                                }
                            }
                        skipped2 = dest;
                        state2 = (dest == null ? 2 : 1);
                        if (dest == maxVia) {
                            int got = Math.min(Math.max(min1p, max2), Math.max(minFromMax2, max3));
                            res = got - 1;
                        } else if (dest == max2Via) {
                            int got = Math.min(Math.max(min1p, max), Math.max(minFromMax, max3));
                            res = got - 1;
                        } else if (dest == min1pVia) {
                            int got = Math.min(Math.max(minFromMax, max2), Math.max(Math.min(minFromMax2, min2p), max));
                            res = got - 1;
                        } else {
                            int got = Math.min(Math.max(minFromMax, max2), Math.max(Math.min(minFromMax2, min1p), max));
                            res = got - 1;
                        }

                        break;
                    case 1:
                        if (skipped2 == dest) {
                            if (dest == maxVia) {
                                int got = Math.min(Math.max(min1p, max2), Math.max(minFromMax2, max3));
                                res = got - 1;
                            } else if (dest == max2Via) {
                                int got = Math.min(Math.max(min1p, max), Math.max(minFromMax, max3));
                                res = got - 1;
                            } else if (dest == min1pVia) {
                                int got = Math.min(Math.max(minFromMax, max2), Math.max(Math.min(minFromMax2, min2p), max));
                                res = got - 1;
                            } else {
                                int got = Math.min(Math.max(minFromMax, max2), Math.max(Math.min(minFromMax2, min1p), max));
                                res = got - 1;
                            }
                            break;
                        }
                    {
                        TaskF.Vertex child = skipped2;
                        int c = child.getMinPropagate(this);
                        if (child == maxVia) {
                            minFromMax = c;
                        } else if (child == max2Via) {
                            minFromMax2 = c;
                        } else if (c < min1p) {
                            min2p = min1p;
                            min2pVia = min1pVia;
                            min1p = c;
                            min1pVia = child;
                        } else if (c < min2p) {
                            min2p = c;
                            min2pVia = child;
                        }
                        skipped2 = null;
                        state2 = 2;
                    }
                    // FALLSTHROUGH
                    case 2:
                        if (dest == maxVia) {
                            int got = Math.min(Math.max(min1p, max2), Math.max(minFromMax2, max3));
                            res = got - 1;
                        } else if (dest == max2Via) {
                            int got = Math.min(Math.max(min1p, max), Math.max(minFromMax, max3));
                            res = got - 1;
                        } else if (dest == min1pVia) {
                            int got = Math.min(Math.max(minFromMax, max2), Math.max(Math.min(minFromMax2, min2p), max));
                            res = got - 1;
                        } else {
                            int got = Math.min(Math.max(minFromMax, max2), Math.max(Math.min(minFromMax2, min1p), max));
                            res = got - 1;
                        }
                        break;
                    default:
                        throw new RuntimeException();
                }

                if (liked) {
                    if (dest == maxVia) {
                        res = Math.min(res, max2 - 1);
                    } else {
                        res = Math.min(res, max - 1);
                    }
                }

                return Math.max(0, res);
            }

        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}

Submission Info

Submission Time
Task F - Black Radius
User Petr
Language Java8 (OpenJDK 1.8.0)
Score 1900
Code Size 13269 Byte
Status AC
Exec Time 1981 ms
Memory 98740 KB

Judge Result

Set Name Sample Subtask All
Score / Max Score 0 / 0 1300 / 1300 600 / 600
Status
AC × 3
AC × 31
AC × 81
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt
Subtask 0_01.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 2_00.txt, 2_01.txt, 2_02.txt, 2_03.txt, 2_04.txt, 2_05.txt, 2_06.txt, 2_07.txt, 2_08.txt, 2_09.txt, 2_10.txt, 2_11.txt, 2_12.txt, 2_13.txt, 2_14.txt, 2_15.txt, 2_16.txt, 2_17.txt, 2_18.txt, 2_19.txt, 2_20.txt, 2_21.txt, 2_22.txt, 2_23.txt, 2_24.txt, 2_25.txt, 2_26.txt, 2_27.txt, 2_28.txt, 2_29.txt, 2_30.txt, 2_31.txt, 2_32.txt, 2_33.txt, 2_34.txt, 2_35.txt, 2_36.txt, 2_37.txt, 2_38.txt, 2_39.txt, 2_40.txt, 2_41.txt, 2_42.txt, 2_43.txt, 2_44.txt, 2_45.txt, 2_46.txt, 2_47.txt
Case Name Status Exec Time Memory
0_00.txt AC 370 ms 8532 KB
0_01.txt AC 100 ms 8400 KB
0_02.txt AC 100 ms 8532 KB
1_00.txt AC 99 ms 8528 KB
1_01.txt AC 1095 ms 96476 KB
1_02.txt AC 1477 ms 95168 KB
1_03.txt AC 1010 ms 85056 KB
1_04.txt AC 899 ms 82184 KB
1_05.txt AC 756 ms 80688 KB
1_06.txt AC 951 ms 85176 KB
1_07.txt AC 917 ms 84972 KB
1_08.txt AC 988 ms 85876 KB
1_09.txt AC 1375 ms 91636 KB
1_10.txt AC 1032 ms 88376 KB
1_11.txt AC 1981 ms 96848 KB
1_12.txt AC 1014 ms 87144 KB
1_13.txt AC 990 ms 91012 KB
1_14.txt AC 975 ms 88376 KB
1_15.txt AC 954 ms 86836 KB
1_16.txt AC 1195 ms 90156 KB
1_17.txt AC 1088 ms 91068 KB
1_18.txt AC 948 ms 84872 KB
1_19.txt AC 924 ms 84008 KB
1_20.txt AC 967 ms 82580 KB
1_21.txt AC 992 ms 86104 KB
1_22.txt AC 879 ms 81784 KB
1_23.txt AC 901 ms 84780 KB
1_24.txt AC 949 ms 82868 KB
1_25.txt AC 1042 ms 83976 KB
1_26.txt AC 877 ms 81112 KB
1_27.txt AC 864 ms 81388 KB
1_28.txt AC 845 ms 81716 KB
1_29.txt AC 929 ms 92024 KB
2_00.txt AC 103 ms 8532 KB
2_01.txt AC 1869 ms 98740 KB
2_02.txt AC 1689 ms 97072 KB
2_03.txt AC 1626 ms 96388 KB
2_04.txt AC 1308 ms 93072 KB
2_05.txt AC 1788 ms 91792 KB
2_06.txt AC 1414 ms 94416 KB
2_07.txt AC 1004 ms 85264 KB
2_08.txt AC 1014 ms 85312 KB
2_09.txt AC 1012 ms 85072 KB
2_10.txt AC 905 ms 82264 KB
2_11.txt AC 922 ms 81940 KB
2_12.txt AC 931 ms 85176 KB
2_13.txt AC 775 ms 80812 KB
2_14.txt AC 763 ms 81664 KB
2_15.txt AC 762 ms 81360 KB
2_16.txt AC 902 ms 82192 KB
2_17.txt AC 991 ms 84636 KB
2_18.txt AC 925 ms 84772 KB
2_19.txt AC 929 ms 84796 KB
2_20.txt AC 936 ms 85312 KB
2_21.txt AC 914 ms 84880 KB
2_22.txt AC 953 ms 82960 KB
2_23.txt AC 980 ms 86408 KB
2_24.txt AC 956 ms 84160 KB
2_25.txt AC 1007 ms 93512 KB
2_26.txt AC 962 ms 92176 KB
2_27.txt AC 1494 ms 92996 KB
2_28.txt AC 1135 ms 88940 KB
2_29.txt AC 1241 ms 94292 KB
2_30.txt AC 1686 ms 94608 KB
2_31.txt AC 1087 ms 89004 KB
2_32.txt AC 937 ms 85444 KB
2_33.txt AC 1205 ms 88372 KB
2_34.txt AC 1372 ms 89992 KB
2_35.txt AC 1163 ms 92292 KB
2_36.txt AC 1006 ms 84084 KB
2_37.txt AC 930 ms 84148 KB
2_38.txt AC 931 ms 83196 KB
2_39.txt AC 994 ms 84904 KB
2_40.txt AC 929 ms 85080 KB
2_41.txt AC 925 ms 84280 KB
2_42.txt AC 902 ms 84464 KB
2_43.txt AC 949 ms 87872 KB
2_44.txt AC 842 ms 80884 KB
2_45.txt AC 822 ms 80976 KB
2_46.txt AC 903 ms 81556 KB
2_47.txt AC 1097 ms 90372 KB