Submission #3222892


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");
//        } else {
//            out.println("Second");
//        }

            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 5925 Byte
Status WA
Exec Time 538 ms
Memory 73112 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 196 ms 56728 KB
in10.txt AC 192 ms 54648 KB
in11.txt WA 505 ms 66668 KB
in12.txt WA 240 ms 26644 KB
in13.txt WA 493 ms 70040 KB
in14.txt WA 457 ms 68100 KB
in15.txt WA 469 ms 66720 KB
in16.txt WA 494 ms 68696 KB
in17.txt WA 467 ms 70460 KB
in18.txt WA 445 ms 66892 KB
in19.txt WA 467 ms 68548 KB
in2.txt AC 188 ms 53496 KB
in20.txt WA 463 ms 67228 KB
in21.txt AC 199 ms 56240 KB
in22.txt AC 184 ms 56832 KB
in23.txt AC 191 ms 55892 KB
in24.txt AC 220 ms 55152 KB
in25.txt AC 186 ms 56916 KB
in26.txt AC 502 ms 68824 KB
in27.txt AC 197 ms 55796 KB
in28.txt AC 159 ms 26452 KB
in29.txt AC 491 ms 70764 KB
in3.txt AC 182 ms 57020 KB
in30.txt AC 511 ms 73112 KB
in31.txt AC 511 ms 69880 KB
in32.txt AC 538 ms 68972 KB
in33.txt AC 498 ms 68536 KB
in4.txt AC 183 ms 55228 KB
in5.txt AC 182 ms 58128 KB
in6.txt AC 203 ms 55316 KB
in7.txt AC 182 ms 54712 KB
in8.txt AC 182 ms 55220 KB
in9.txt AC 183 ms 55708 KB
sample1.txt AC 71 ms 20820 KB
sample2.txt AC 72 ms 20820 KB
sample3.txt AC 159 ms 26196 KB