Submission #3222960
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.io.Writer; import java.util.Set; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.TreeSet; 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"); while (leaves.size() > 0) { int u = leaves.first(); if (g[u].size() == 0) { leaves.remove(u); continue; } int v = ((TreeSet<Integer>) g[u]).first(); g[v].remove(u); for (int ver : ((TreeSet<Integer>) g[v])) { if (leaves.contains(ver)) { out.println("First"); return; } g[ver].remove(v); if (g[ver].size() == 1) { leaves.add(ver); } } leaves.remove(u); } 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 | 900 |
Code Size | 6596 Byte |
Status | AC |
Exec Time | 368 ms |
Memory | 65852 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 | 900 / 900 | ||||
Status |
|
|
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 | 194 ms | 54664 KB |
in10.txt | AC | 173 ms | 55140 KB |
in11.txt | AC | 348 ms | 59064 KB |
in12.txt | AC | 139 ms | 23252 KB |
in13.txt | AC | 346 ms | 65852 KB |
in14.txt | AC | 365 ms | 64324 KB |
in15.txt | AC | 364 ms | 62960 KB |
in16.txt | AC | 315 ms | 61632 KB |
in17.txt | AC | 332 ms | 59792 KB |
in18.txt | AC | 332 ms | 61584 KB |
in19.txt | AC | 337 ms | 54332 KB |
in2.txt | AC | 187 ms | 56568 KB |
in20.txt | AC | 318 ms | 59880 KB |
in21.txt | AC | 183 ms | 57088 KB |
in22.txt | AC | 195 ms | 56440 KB |
in23.txt | AC | 178 ms | 56136 KB |
in24.txt | AC | 208 ms | 53112 KB |
in25.txt | AC | 191 ms | 54812 KB |
in26.txt | AC | 291 ms | 55292 KB |
in27.txt | AC | 194 ms | 56724 KB |
in28.txt | AC | 71 ms | 21332 KB |
in29.txt | AC | 361 ms | 61908 KB |
in3.txt | AC | 171 ms | 53080 KB |
in30.txt | AC | 368 ms | 62800 KB |
in31.txt | AC | 294 ms | 56056 KB |
in32.txt | AC | 337 ms | 56368 KB |
in33.txt | AC | 307 ms | 57100 KB |
in4.txt | AC | 183 ms | 52816 KB |
in5.txt | AC | 184 ms | 59060 KB |
in6.txt | AC | 182 ms | 53476 KB |
in7.txt | AC | 184 ms | 55924 KB |
in8.txt | AC | 183 ms | 56156 KB |
in9.txt | AC | 204 ms | 55008 KB |
sample1.txt | AC | 71 ms | 18900 KB |
sample2.txt | AC | 71 ms | 20052 KB |
sample3.txt | AC | 70 ms | 19412 KB |