Submission #3222901


Source Code Expand

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.Set;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.TreeSet;
import java.util.Map;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

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

    static class agc014d {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt();

            Set[] g = new Set[n];

            for (int i = 0; i < n; i++) {
                g[i] = new TreeSet();
            }

            for (int i = 0; i < n - 1; i++) {
                int a = in.nextInt();
                int b = in.nextInt();

                a--;
                b--;

                g[a].add(b);
                g[b].add(a);
            }

            TreeSet<Integer> leaves = new TreeSet<>();

            for (int i = 0; i < n; i++) {
                int l = 0;
                for (int v : (Set<Integer>) g[i]) {
                    if (g[v].size() == 1) {
                        l++;
                        leaves.add(v);
                    }
                }

                if (l > 1) {
                    out.println("First");
                    return;
                }
            }

            if (n % 2 == 1) {
                out.println("First");
                return;
            }

            Map<Integer, Integer> mp = new HashMap<>();

            int rn = n;


            int lev = 0;
            while (rn > 0) {
                Map<Integer, Integer> nmp = new HashMap<>();

                TreeSet<Integer> nleaves = new TreeSet<>();

                for (int l : leaves) {
                    if (g[l].size() <= 1) {
                        if (g[l].size() == 1) {
                            int v = ((TreeSet<Integer>) g[l]).first();
                            g[v].remove(l);
                            nmp.merge(v, 1, (x, y) -> x + y);
                            nleaves.add(v);
                        }
                        rn--;
                        if (lev % 2 == 1 && mp.getOrDefault(l, 0) > 1) {
                            out.println("First");
                            return;
                        }
                    }
                }

                leaves = nleaves;
                mp = nmp;
                lev++;
            }

            out.println("Second");
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
        }

        public void println(Object... objects) {
            print(objects);
            writer.println();
        }

        public void close() {
            writer.close();
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}

Submission Info

Submission Time
Task D - Black and White Tree
User leashptntl
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 5900 Byte
Status WA
Exec Time 530 ms
Memory 73024 KB

Compile Error

Note: ./Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 900
Status
AC × 3
AC × 29
WA × 10
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in3.txt, in30.txt, in31.txt, in32.txt, in33.txt, in4.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, sample1.txt, sample2.txt, sample3.txt
Case Name Status Exec Time Memory
in1.txt AC 183 ms 55560 KB
in10.txt AC 180 ms 55124 KB
in11.txt WA 487 ms 67596 KB
in12.txt WA 218 ms 27832 KB
in13.txt WA 462 ms 68408 KB
in14.txt WA 496 ms 71152 KB
in15.txt WA 457 ms 65760 KB
in16.txt WA 492 ms 68280 KB
in17.txt WA 486 ms 73024 KB
in18.txt WA 486 ms 68932 KB
in19.txt WA 474 ms 68872 KB
in2.txt AC 183 ms 56492 KB
in20.txt WA 530 ms 69800 KB
in21.txt AC 186 ms 56004 KB
in22.txt AC 183 ms 54612 KB
in23.txt AC 190 ms 54636 KB
in24.txt AC 218 ms 54592 KB
in25.txt AC 196 ms 58336 KB
in26.txt AC 509 ms 68532 KB
in27.txt AC 224 ms 56604 KB
in28.txt AC 146 ms 23504 KB
in29.txt AC 240 ms 54908 KB
in3.txt AC 181 ms 53440 KB
in30.txt AC 233 ms 55560 KB
in31.txt AC 461 ms 68692 KB
in32.txt AC 508 ms 71284 KB
in33.txt AC 459 ms 66144 KB
in4.txt AC 184 ms 54224 KB
in5.txt AC 183 ms 54280 KB
in6.txt AC 187 ms 54612 KB
in7.txt AC 172 ms 56840 KB
in8.txt AC 185 ms 56880 KB
in9.txt AC 189 ms 55416 KB
sample1.txt AC 71 ms 19540 KB
sample2.txt AC 72 ms 18900 KB
sample3.txt AC 146 ms 25556 KB