結果

問題 No.241 出席番号(1)
ユーザー uafr_csuafr_cs
提出日時 2015-09-11 23:48:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 2,499 bytes
コンパイル時間 3,819 ms
コンパイル使用メモリ 75,880 KB
実行使用メモリ 56,368 KB
最終ジャッジ日時 2023-08-20 17:52:58
合計ジャッジ時間 8,465 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 101 ms
56,368 KB
testcase_01 AC 103 ms
56,296 KB
testcase_02 AC 104 ms
55,684 KB
testcase_03 AC 99 ms
55,900 KB
testcase_04 AC 107 ms
55,956 KB
testcase_05 AC 101 ms
55,996 KB
testcase_06 AC 100 ms
55,860 KB
testcase_07 AC 100 ms
56,156 KB
testcase_08 AC 102 ms
55,632 KB
testcase_09 AC 100 ms
55,748 KB
testcase_10 AC 101 ms
56,168 KB
testcase_11 AC 100 ms
55,864 KB
testcase_12 AC 100 ms
53,780 KB
testcase_13 AC 104 ms
56,228 KB
testcase_14 AC 124 ms
56,132 KB
testcase_15 AC 118 ms
55,676 KB
testcase_16 AC 102 ms
55,948 KB
testcase_17 AC 101 ms
56,320 KB
testcase_18 AC 102 ms
55,984 KB
testcase_19 AC 102 ms
55,640 KB
testcase_20 AC 106 ms
56,156 KB
testcase_21 AC 102 ms
56,196 KB
testcase_22 AC 102 ms
55,908 KB
testcase_23 AC 111 ms
56,088 KB
testcase_24 AC 116 ms
56,224 KB
testcase_25 AC 117 ms
56,164 KB
testcase_26 AC 116 ms
55,868 KB
testcase_27 AC 112 ms
56,076 KB
testcase_28 AC 127 ms
56,216 KB
testcase_29 AC 135 ms
56,328 KB
testcase_30 AC 133 ms
55,860 KB
testcase_31 AC 117 ms
56,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;

public class Main {
	
public static final int INF = Integer.MAX_VALUE / 2 - 1;
	
	public static int dinic(Vertex s, Vertex t) {
		int flow = 0;
		for (int p = 1;; p++) {
			Queue<Vertex> que = new LinkedList<Vertex>();
			s.level = 0;
			s.p = p;
			que.offer(s);
			
			while (!que.isEmpty()) {
				Vertex v = que.poll();
				v.iter = v.es.size() - 1;
				for (Edge e : v.es)
					if (e.to.p < p && e.cap > 0) {
						e.to.level = v.level + 1;
						e.to.p = p;
						que.offer(e.to);
					}
			}
			
			if (t.p < p){
				return flow;
			}
			
			for (int f; (f = dfs(s, t, INF)) > 0;){
				flow += f;
			}
		}
	}

	public static int dfs(Vertex v, Vertex t, int f) {
		if (v == t) {
			return f;
		}
		
		for (; v.iter >= 0; v.iter--) {
			Edge e = v.es.get(v.iter);
			if (v.level < e.to.level && e.cap > 0) {
				int d = dfs(e.to, t, Math.min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					e.rev.cap += d;
					return d;
				}
			}
		}
		
		return 0;
	}

	public static class Vertex {
		ArrayList<Edge> es = new ArrayList<Edge>();
		int level, p, iter;
		int id;
		
		public Vertex(int id){ this.id = id; }

		void add(Vertex to, int cap) {
			Edge e = new Edge(to, cap), rev = new Edge(this, 0);
			e.rev = rev;
			rev.rev = e;
			es.add(e);
			to.es.add(rev);
		}
	}

	public static class Edge {
		Vertex to;
		Edge rev;
		int cap;

		Edge(Vertex to, int cap) {
			this.to = to;
			this.cap = cap;
		}
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		int[] number = new int[N];
		for(int i = 0; i < N; i++){
			number[i] = sc.nextInt();
		}
		
		Vertex s = new Vertex(-1);
		Vertex t = new Vertex(-1);
		
		Vertex[] from = new Vertex[N];
		Vertex[] to = new Vertex[N];
		for(int i = 0; i < N; i++){
			from[i] = new Vertex(i);
			to[i] = new Vertex(i);
		}
		
		for(int i = 0; i < N; i++){
			s.add(from[i], 1);
			to[i].add(t, 1);
		}
		
		for(int i = 0; i < N; i++){
			for(int j = 0; j < N; j++){
				if(number[i] != j){
					from[i].add(to[j], 1);
				}
			}
		}
		
		final int flow = dinic(s, t);
		
		if(flow < N){
			System.out.println(-1);
		}else{
			LOOP:
			for(int i = 0; i < N; i++){
				for(Edge e : from[i].es){
					if(e.cap == 0){
						System.out.println(e.to.id);
						continue LOOP;
					}
				}
			}
		}
		
		
	}

}
0