Submission #3063391
Source Code Expand
// package other2015.utpc2014; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; public class Main { static long __startTime = System.currentTimeMillis(); static Vertex[] graph; static void dfs(Vertex v, Vertex p) { v.count = 1; v.parent = p == null ? -1 : p.idx; for (Edge e : v.adj) { int to = e.u + e.v - v.idx; if (p != null && to == p.idx) { continue; } dfs(graph[to], v); v.count += graph[to].count; } } static List<Edge> tmpedges; static void dfsFrom(Vertex v, Vertex p) { for (Edge e : v.adj) { int to = e.u + e.v - v.idx; if (p != null && to == p.idx) { continue; } tmpedges.add(masterEdges[e.idx]); dfsFrom(graph[to], v); } } static class Group { TreeSet<Edge> edges; int root; public Group(List<Edge> e, int r) { edges = new TreeSet<>(e); root = r; dfs(graph[root], null); } public void operate(int e, int s, int d) { Edge ed = edges.ceiling(new Edge(e, -1, -1, s)); edges.remove(ed); ed.strength += d; edges.add(ed); } public int[] cutmin() { Edge min = edges.first(); edges.remove(min); Vertex newRoot = graph[min.u].parent == min.v ? graph[min.u] : graph[min.v]; Vertex newRootParent = graph[newRoot.parent]; Edge mine = new Edge(min.idx, -1, -1, 0); newRoot.adj.remove(mine); newRootParent.adj.remove(mine); tmpedges = new ArrayList<>(); if (graph[root].count < newRoot.count * 2) { dfsFrom(graph[root], null); for (Edge e : tmpedges) { edges.remove(e); } int newSmallRoot = root; root = newRoot.idx; return new int[]{newSmallRoot, min.idx}; } else { dfsFrom(newRoot, null); for (Edge e : tmpedges) { edges.remove(e); } return new int[]{newRoot.idx, min.idx}; } } } static class Vertex { TreeSet<Edge> adj; int idx; int parent; int count; public Vertex(int id) { idx = id; adj = new TreeSet<>(); parent = -1; count = 0; } } static class Edge implements Comparable<Edge> { int idx; int u; int v; int strength; public Edge(int i, int a, int b, int c) { idx = i; u = a; v = b; strength = c; } @Override public int compareTo(Edge o) { return strength == o.strength ? idx - o.idx : strength - o.strength; } } static Edge[] masterEdges; public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int[] edgeId = new int[n-1]; int[] str = new int[n-1]; List<Edge> edges = new ArrayList<>(); List<Group> g = new ArrayList<>(); masterEdges = new Edge[n-1]; for (int i = 0; i < n-1; i++) { Edge e = new Edge(i, in.nextInt()-1, in.nextInt()-1, in.nextInt()); masterEdges[i] = e; edges.add(e); str[i] = e.strength; } graph = new Vertex[n]; for (int i = 0; i < n ; i++) { graph[i] = new Vertex(i); } for (Edge e : edges) { Edge we = new Edge(e.idx, e.u, e.v, 0); graph[e.u].adj.add(we); graph[e.v].adj.add(we); } g.add(new Group(edges, 0)); int q = in.nextInt(); while (--q >= 0) { int type = in.nextInt(); int e = in.nextInt()-1; int gid = edgeId[e]; if (type == 1) { int d = in.nextInt(); if (gid == -1) { continue; } g.get(gid).operate(e, str[e], d); str[e] += d; } else { if (gid == -1) { out.println(-1); } else { int[] pair = g.get(gid).cutmin(); edgeId[pair[1]] = -1; int ngid = g.size(); for (Edge ed : tmpedges) { edgeId[ed.idx] = ngid; ed.strength = str[ed.idx]; } g.add(new Group(tmpedges, pair[0])); out.println(pair[1]+1); } } } out.flush(); } private static void printTime(String label) { debug(label, System.currentTimeMillis() - __startTime); } public static void debug(Object... o) { System.err.println(Arrays.deepToString(o)); } public static class InputReader { private static final int BUFFER_LENGTH = 1 << 12; private InputStream stream; private byte[] buf = new byte[BUFFER_LENGTH]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int next() { 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 char nextChar() { return (char) skipWhileSpace(); } public String nextToken() { int c = skipWhileSpace(); StringBuilder res = new StringBuilder(); do { res.append((char) c); c = next(); } while (!isSpaceChar(c)); return res.toString(); } public int nextInt() { return (int) nextLong(); } public long nextLong() { int c = skipWhileSpace(); long sgn = 1; if (c == '-') { sgn = -1; c = next(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = next(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { return Double.valueOf(nextToken()); } int skipWhileSpace() { int c = next(); while (isSpaceChar(c)) { c = next(); } return c; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Submission Info
Submission Time | |
---|---|
Task | I - 盆栽 |
User | hamadu |
Language | Java8 (OpenJDK 1.8.0) |
Score | 300 |
Code Size | 7739 Byte |
Status | AC |
Exec Time | 1452 ms |
Memory | 115940 KB |
Judge Result
Set Name | Subtask | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 30 / 30 | 270 / 270 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Subtask | atetubou-challenge1.in, subtask_00.txt, subtask_01.txt, subtask_02.txt, subtask_03.txt, subtask_04.txt, subtask_05.txt, subtask_06.txt, subtask_07.txt, subtask_08.txt, subtask_09.txt, subtask_10.txt, subtask_11.txt, subtask_12.txt, subtask_13.txt, subtask_14.txt, subtask_15.txt, subtask_16.txt |
All | scrambled_00.txt, scrambled_01.txt, scrambled_02.txt, scrambled_03.txt, scrambled_04.txt, scrambled_05.txt, scrambled_06.txt, scrambled_07.txt, scrambled_08.txt, scrambled_09.txt, scrambled_10.txt, scrambled_11.txt, scrambled_12.txt, scrambled_13.txt, scrambled_14.txt, scrambled_15.txt, scrambled_16.txt, scrambled_17.txt, scrambled_18.txt, scrambled_19.txt, scrambled_20.txt, scrambled_21.txt, scrambled_22.txt, scrambled_23.txt, scrambled_24.txt, scrambled_25.txt, scrambled_26.txt, scrambled_27.txt, scrambled_28.txt, scrambled_29.txt, scrambled_30.txt, scrambled_31.txt, scrambled_32.txt, subtask_00.txt, subtask_01.txt, subtask_02.txt, subtask_03.txt, subtask_04.txt, subtask_05.txt, subtask_06.txt, subtask_07.txt, subtask_08.txt, subtask_09.txt, subtask_10.txt, subtask_11.txt, subtask_12.txt, subtask_13.txt, subtask_14.txt, subtask_15.txt, subtask_16.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
scrambled_00.txt | AC | 73 ms | 21204 KB |
scrambled_01.txt | AC | 113 ms | 18772 KB |
scrambled_02.txt | AC | 112 ms | 21332 KB |
scrambled_03.txt | AC | 119 ms | 19152 KB |
scrambled_04.txt | AC | 112 ms | 21764 KB |
scrambled_05.txt | AC | 119 ms | 23444 KB |
scrambled_06.txt | AC | 110 ms | 21332 KB |
scrambled_07.txt | AC | 111 ms | 21332 KB |
scrambled_08.txt | AC | 116 ms | 19540 KB |
scrambled_09.txt | AC | 120 ms | 20644 KB |
scrambled_10.txt | AC | 114 ms | 23060 KB |
scrambled_11.txt | AC | 1206 ms | 115940 KB |
scrambled_12.txt | AC | 1175 ms | 103924 KB |
scrambled_13.txt | AC | 1286 ms | 113656 KB |
scrambled_14.txt | AC | 1386 ms | 110364 KB |
scrambled_15.txt | AC | 1214 ms | 109216 KB |
scrambled_16.txt | AC | 1248 ms | 109680 KB |
scrambled_17.txt | AC | 1128 ms | 106508 KB |
scrambled_18.txt | AC | 1209 ms | 109000 KB |
scrambled_19.txt | AC | 1252 ms | 110184 KB |
scrambled_20.txt | AC | 1305 ms | 111008 KB |
scrambled_21.txt | AC | 1032 ms | 107752 KB |
scrambled_22.txt | AC | 128 ms | 21428 KB |
scrambled_23.txt | AC | 124 ms | 21812 KB |
scrambled_24.txt | AC | 125 ms | 24012 KB |
scrambled_25.txt | AC | 118 ms | 22420 KB |
scrambled_26.txt | AC | 121 ms | 22708 KB |
scrambled_27.txt | AC | 1277 ms | 106940 KB |
scrambled_28.txt | AC | 1450 ms | 108200 KB |
scrambled_29.txt | AC | 1342 ms | 103580 KB |
scrambled_30.txt | AC | 1142 ms | 101456 KB |
scrambled_31.txt | AC | 1452 ms | 108504 KB |
scrambled_32.txt | AC | 70 ms | 19540 KB |
subtask_00.txt | AC | 71 ms | 18388 KB |
subtask_01.txt | AC | 115 ms | 22740 KB |
subtask_02.txt | AC | 112 ms | 24660 KB |
subtask_03.txt | AC | 108 ms | 20820 KB |
subtask_04.txt | AC | 115 ms | 20564 KB |
subtask_05.txt | AC | 112 ms | 20692 KB |
subtask_06.txt | AC | 116 ms | 21972 KB |
subtask_07.txt | AC | 109 ms | 22356 KB |
subtask_08.txt | AC | 120 ms | 20816 KB |
subtask_09.txt | AC | 121 ms | 21844 KB |
subtask_10.txt | AC | 115 ms | 21588 KB |
subtask_11.txt | AC | 122 ms | 22116 KB |
subtask_12.txt | AC | 122 ms | 21940 KB |
subtask_13.txt | AC | 125 ms | 22068 KB |
subtask_14.txt | AC | 120 ms | 24520 KB |
subtask_15.txt | AC | 121 ms | 24548 KB |
subtask_16.txt | AC | 71 ms | 21588 KB |