Submission #363635
Source Code Expand
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; public class Main { static class SCC { boolean[] visited; int[] node_id; List<Integer> rev; int n; int[][] graph; int[][] r_graph; SCC(int[][] g) { n = g.length; graph = g; r_graph = new int[n][]; int[] deg = new int[n]; for (int i = 0 ; i < n ; i++) { for (int j : graph[i]) { deg[j]++; } } for (int i = 0 ; i < n ; i++) { r_graph[i] = new int[deg[i]]; } for (int i = 0 ; i < n ; i++) { for (int j : graph[i]) { r_graph[j][--deg[j]] = i; } } } int[] scc() { visited = new boolean[n]; rev = new ArrayList<Integer>(); for (int i = 0; i<n; i++) { if (!visited[i]) { dfs(i); } } int id = 0; node_id = new int[n]; visited = new boolean[n]; for (int i = rev.size()-1; i>=0; i--) { if (!visited[rev.get(i)]) { rdfs(rev.get(i), id); id++; } } return node_id; } int[][] groups() { int max = 0; for (int nid : node_id) { max = Math.max(max, nid+1); } int[] gnum = new int[max]; for (int nid : node_id) { gnum[nid]++; } int[][] groups = new int[max][]; for (int i = 0 ; i < max ; i++) { groups[i] = new int[gnum[i]]; } for (int i = 0 ; i < n ; i++) { int nid = node_id[i]; groups[nid][--gnum[nid]] = i; } return groups; } public void dfs(int i) { visited[i] = true; for (int next : graph[i]) { if (!visited[next]) { dfs(next); } } rev.add(i); } public void rdfs(int i, int id) { visited[i] = true; node_id[i] = id; for (int next : r_graph[i]) { if (!visited[next]) { rdfs(next, id); } } } public int[][] groupGraph() { int max = 0; for (int nid : node_id) { max = Math.max(max, nid+1); } Set<Integer>[] eset = new Set[max]; List<Integer>[] ged = new List[max]; for (int i = 0; i < max; i++) { eset[i] = new HashSet<>(); ged[i] = new ArrayList<>(); } int[][] groupGraph = new int[max][]; for (int fr = 0 ; fr < graph.length ; fr++) { for (int to : graph[fr]) { int g1 = node_id[fr]; int g2 = node_id[to]; if (g1 != g2 && !eset[g1].contains(g2)) { eset[g1].add(g2); ged[g1].add(g2); } } } for (int i = 0 ; i < max ; i++) { groupGraph[i] = new int[ged[i].size()]; int idx = 0; for (int to : ged[i]) { groupGraph[i][idx++] = to; } } return groupGraph; } } public static class State implements Comparable<State> { int gi; String x; State(int gi, String x) { this.gi = gi; this.x = x; } @Override public int compareTo(State o) { return x.compareTo(o.x); } } public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); char[] c = new char[n]; for (int i = 0; i < n; i++) { c[i] = in.nextChar(); } int[][] graph = buildDirectedGraph(in, n, m); SCC scc = new SCC(graph); scc.scc(); int[][] groups = scc.groups(); int[][] groupGraph = scc.groupGraph(); int gn = groups.length; String[] gs = new String[gn]; for (int i = 0 ; i < gn ; i++) { gs[i] = ""; for (int v : groups[i]) { gs[i] += c[v]; } char[] x = gs[i].toCharArray(); Arrays.sort(x); gs[i] = String.valueOf(x); } String best = "~"; String[][] dp = new String[gn][k+1]; for (int i = 0 ; i < gn ; i++) { Arrays.fill(dp[i], "~"); } Queue<State> q = new PriorityQueue<>(); for (int i = 0 ; i < gn ; i++) { dp[i][0] = ""; q.add(new State(i, "")); } // debug(groupGraph, gs); while (q.size() >= 1) { State s = q.poll(); int now = s.gi; int len = gs[now].length(); for (int take = 0 ; take <= len ; take++) { String tx = s.x + gs[now].substring(0, take); int tl = tx.length(); if (tl > k) { break; } if (tl == k && tx.compareTo(best) < 0) { best = tx; } for (int to : groupGraph[now]) { if (tx.compareTo(dp[to][tl]) < 0) { dp[to][tl] = tx; q.add(new State(to, tx)); } } } } out.println(best.equals("~") ? -1 : best); out.flush(); } static int[][] buildDirectedGraph(InputReader in, int n, int m) { int[][] edges = new int[m][]; int[][] graph = new int[n][]; int[] deg = new int[n]; for (int i = 0 ; i < m ; i++) { int a = in.nextInt()-1; int b = in.nextInt()-1; deg[a]++; edges[i] = new int[]{a, b}; } for (int i = 0 ; i < n ; i++) { graph[i] = new int[deg[i]]; } for (int i = 0 ; i < m ; i++) { int a = edges[i][0]; int b = edges[i][1]; graph[a][--deg[a]] = b; } return graph; } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; 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() { int c = next(); while (isSpaceChar(c)) c = next(); if ('a' <= c && c <= 'z') { return (char)c; } if ('A' <= c && c <= 'Z') { return (char)c; } throw new InputMismatchException(); } public int nextInt() { int c = next(); while (isSpaceChar(c)) c = next(); int sgn = 1; if (c == '-') { sgn = -1; c = next(); } int 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 long nextLong() { int c = next(); while (isSpaceChar(c)) c = next(); 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 boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static void debug(Object... o) { System.err.println(Arrays.deepToString(o)); } }
Submission Info
Submission Time | |
---|---|
Task | C - 有向グラフ |
User | hamadu |
Language | Java (OpenJDK 1.7.0) |
Score | 100 |
Code Size | 9653 Byte |
Status | AC |
Exec Time | 589 ms |
Memory | 45388 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 | 100 / 100 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | subtask0_sample_01.txt, subtask0_sample_02.txt, subtask0_sample_03.txt, subtask0_sample_04.txt |
All | subtask0_sample_01.txt, subtask0_sample_02.txt, subtask0_sample_03.txt, subtask0_sample_04.txt, subtask1_manual01.txt, subtask1_manual02.txt, subtask1_manual03.txt, subtask1_manual04.txt, subtask1_manual05.txt, subtask1_manual06.txt, subtask1_manual07.txt, subtask1_manual08.txt, subtask1_random01.txt, subtask1_random02.txt, subtask1_random03.txt, subtask1_random04.txt, subtask1_random05.txt, subtask1_special01.txt, subtask1_special02.txt, subtask1_special03.txt, subtask1_special04.txt, subtask1_special05.txt, subtask1_special06.txt, subtask1_special07.txt, subtask1_special08.txt, subtask1_special09.txt, subtask1_special10.txt, subtask1_special11.txt, subtask1_special12.txt, subtask1_special13.txt, subtask1_special14.txt, subtask1_special15.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
subtask0_sample_01.txt | AC | 330 ms | 21392 KB |
subtask0_sample_02.txt | AC | 325 ms | 21316 KB |
subtask0_sample_03.txt | AC | 326 ms | 21360 KB |
subtask0_sample_04.txt | AC | 313 ms | 21348 KB |
subtask1_manual01.txt | AC | 322 ms | 21296 KB |
subtask1_manual02.txt | AC | 315 ms | 21300 KB |
subtask1_manual03.txt | AC | 313 ms | 21380 KB |
subtask1_manual04.txt | AC | 318 ms | 21328 KB |
subtask1_manual05.txt | AC | 326 ms | 21248 KB |
subtask1_manual06.txt | AC | 314 ms | 21300 KB |
subtask1_manual07.txt | AC | 313 ms | 21340 KB |
subtask1_manual08.txt | AC | 314 ms | 21372 KB |
subtask1_random01.txt | AC | 410 ms | 28652 KB |
subtask1_random02.txt | AC | 370 ms | 23660 KB |
subtask1_random03.txt | AC | 460 ms | 32904 KB |
subtask1_random04.txt | AC | 387 ms | 26628 KB |
subtask1_random05.txt | AC | 312 ms | 21692 KB |
subtask1_special01.txt | AC | 312 ms | 22048 KB |
subtask1_special02.txt | AC | 386 ms | 25088 KB |
subtask1_special03.txt | AC | 487 ms | 36944 KB |
subtask1_special04.txt | AC | 545 ms | 41772 KB |
subtask1_special05.txt | AC | 589 ms | 45388 KB |
subtask1_special06.txt | AC | 546 ms | 38964 KB |
subtask1_special07.txt | AC | 540 ms | 37316 KB |
subtask1_special08.txt | AC | 518 ms | 37876 KB |
subtask1_special09.txt | AC | 536 ms | 38860 KB |
subtask1_special10.txt | AC | 522 ms | 37588 KB |
subtask1_special11.txt | AC | 542 ms | 38016 KB |
subtask1_special12.txt | AC | 519 ms | 36764 KB |
subtask1_special13.txt | AC | 525 ms | 36596 KB |
subtask1_special14.txt | AC | 317 ms | 21620 KB |
subtask1_special15.txt | AC | 574 ms | 44628 KB |