結果
問題 | No.241 出席番号(1) |
ユーザー | tanzaku |
提出日時 | 2015-07-10 22:37:26 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 54 ms / 2,000 ms |
コード長 | 8,071 bytes |
コンパイル時間 | 2,585 ms |
コンパイル使用メモリ | 91,428 KB |
実行使用メモリ | 38,236 KB |
最終ジャッジ日時 | 2024-11-30 21:20:11 |
合計ジャッジ時間 | 5,393 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
import java.io.*;import java.math.*;import java.util.*;import static java.util.Arrays.*;public class Main {private static final int mod = (int)1e9+7;final Random random = new Random(0);final IOFast io = new IOFast();/// MAIN CODEpublic void run() throws IOException {// int TEST_CASE = Integer.parseInt(new String(io.nextLine()).trim());int TEST_CASE = 1;while(TEST_CASE-- != 0) {final int S = 50 * 2, T = S + 1;Dinic flow = new Dinic(T + 1, T * T * 2 + 1000);int n = io.nextInt();int[] A = io.nextIntArray(n);for(int i = 0; i < n; i++) {flow.AddEdge(S, i, 1);flow.AddEdge(i + n, T, 1);for(int j = 0; j < n; j++) {if(A[i] != j) {flow.AddEdge(j, i + n, 1);}}}if(flow.MaxFlow(S, T) != n) {io.out.println(-1);return;}int[] ans = new int[n];for(int v = 0; v < n; v++) {for(int e = flow.g.head[v]; e != -1; e = flow.g.next[e]) {if(flow.g.to[e] == S) continue;if(flow.g.cap[e] == 0 && flow.g.cap[flow.g.rev[e]] != 0) {ans[flow.g.to[e] - n] = v;break;}}}for(int v : ans) {io.out.println(v);}}}// aristaticclass Dinic {final int INF = 1 << 29;// List<List<Edge>> G;AdjListGraph g;int[] level, iter, q;void bfs(int s) {for (int i = 0; i < level.length; i++) level[i] = -1;// Queue<Integer> q = new LinkedList<Integer>();int qs = 0, qt = 0;level[s] = 0;q[qt++] =s;while (qs != qt) {int v = q[qs++];for(int e = g.head[v]; e != -1; e = g.next[e]) {final int t = g.to[e];if (g.cap[e] > 0 && level[t] < 0) {level[t] = level[v] + 1;q[qt++] = t;}}}}int dfs(int v, int t, int f) {if (v == t) return f;for (int i = iter[v]; i != -1; i = iter[v] = g.next[i]) {if (g.cap[i] > 0 && level[v] < level[g.to[i]]) {int d = dfs(g.to[i], t, Math.min(f, g.cap[i]));if (d > 0) {g.cap[i] -= d;g.cap[g.rev[i]] += d;return d;}}}return 0;}public void AddEdge(int from, int to, int cap) {g.addEdge(from, to, cap, g.m + 1);g.addEdge(to, from, 0, g.m - 1);}public int MaxFlow(int s, int t) {int flow = 0;for (; ; ) {bfs(s);if (level[t] < 0) return flow;for (int i = 0; i < iter.length; i++) iter[i] = g.head[i];for (int f = 0; (f = dfs(s, t, INF)) > 0; )flow += f;}}public int BipartiteMatching(int s, int t) {return MaxFlow(s, t);}void clear() {g.clear();}void clear(int n) {g.clear(n);}public Dinic(int size, int e) {level = new int[size];iter = new int[size];q = new int[size];g = new AdjListGraph(size, 2 * e);}static class AdjListGraph {int m;int[] head, next, to, rev;int[] cap;public AdjListGraph(int V, int E) {head = new int[V];next = new int[E];to = new int[E];cap = new int[E];rev = new int[E];clear();}public void clear(int v) {m = 0;for(int i = 0; i < v; i++) {head[i] = -1;}}public void clear() {m = 0;Arrays.fill(head, -1);}public void addEdge(int s, int t, int c, int r) {next[m] = head[s];head[s] = m;to[m] = t;cap[m] = c;rev[m++] = r;}}}/// TEMPLATEstatic int gcd(int n, int r) { return r == 0 ? n : gcd(r, n%r); }static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n%r); }static <T> void swap(T[] x, int i, int j) { T t = x[i]; x[i] = x[j]; x[j] = t; }static void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t; }void main() throws IOException {// IOFast.setFileIO("rle-size.in", "rle-size.out");try { run(); }catch (EndOfFileRuntimeException e) { }io.out.flush();}public static void main(String[] args) throws IOException { new Main().main(); }static class EndOfFileRuntimeException extends RuntimeException {private static final long serialVersionUID = -8565341110209207657L; }staticpublic class IOFast {private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));private PrintWriter out = new PrintWriter(System.out);void setFileIn(String ins) throws IOException { in.close(); in = new BufferedReader(new FileReader(ins)); }void setFileOut(String outs) throws IOException { out.flush(); out.close(); out = new PrintWriter(new FileWriter(outs)); }void setFileIO(String ins, String outs) throws IOException { setFileIn(ins); setFileOut(outs); }private static int pos, readLen;private static final char[] buffer = new char[1024 * 8];private static char[] str = new char[500*8*2];private static boolean[] isDigit = new boolean[256];private static boolean[] isSpace = new boolean[256];private static boolean[] isLineSep = new boolean[256];static { for(int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] =isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; }public int read() throws IOException { if(pos >= readLen) { pos = 0; readLen = in.read(buffer); if(readLen <= 0) { throw newEndOfFileRuntimeException(); } } return buffer[pos++]; }public int nextInt() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; int ret = 0; if(str[0]== '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; }public long nextLong() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; long ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; }public char nextChar() throws IOException { while(true) { final int c = read(); if(!isSpace[c]) { return (char)c; } } }int reads(int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } if(str.length ==len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char)c; } }catch(EndOfFileRuntimeException e) { ; } return len; }int reads(char[] cs, int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; }cs[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; }public char[] nextLine() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isLineSep); try { if(str[len-1] == '\r'){ len--; read(); } } catch(EndOfFileRuntimeException e) { ; } return Arrays.copyOf(str, len); }public String nextString() throws IOException { return new String(next()); }public char[] next() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); return Arrays.copyOf(str, len); }public int next(char[] cs) throws IOException { int len = 0; cs[len++] = nextChar(); len = reads(cs, len, isSpace); return len; }public double nextDouble() throws IOException { return Double.parseDouble(nextString()); }public long[] nextLongArray(final int n) throws IOException { final long[] res = new long[n]; for(int i = 0; i < n; i++) { res[i] = nextLong(); } return res; }public int[] nextIntArray(final int n) throws IOException { final int[] res = new int[n]; for(int i = 0; i < n; i++) { res[i] = nextInt(); }return res; }public int[][] nextIntArray2D(final int n, final int k) throws IOException { final int[][] res = new int[n][]; for(int i = 0; i < n; i++) {res[i] = nextIntArray(k); } return res; }public int[][] nextIntArray2DWithIndex(final int n, final int k) throws IOException { final int[][] res = new int[n][k+1]; for(int i = 0; i <n; i++) { for(int j = 0; j < k; j++) { res[i][j] = nextInt(); } res[i][k] = i; } return res; }public double[] nextDoubleArray(final int n) throws IOException { final double[] res = new double[n]; for(int i = 0; i < n; i++) { res[i] =nextDouble(); } return res; }}}