結果

問題 No.483 マッチ並べ
ユーザー uafr_csuafr_cs
提出日時 2017-02-10 23:21:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 2,444 bytes
コンパイル時間 2,285 ms
コンパイル使用メモリ 77,772 KB
実行使用メモリ 57,892 KB
最終ジャッジ日時 2023-08-28 12:23:35
合計ジャッジ時間 12,136 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
56,128 KB
testcase_01 AC 125 ms
55,980 KB
testcase_02 AC 122 ms
56,104 KB
testcase_03 AC 127 ms
55,752 KB
testcase_04 AC 127 ms
55,828 KB
testcase_05 AC 129 ms
55,736 KB
testcase_06 AC 129 ms
54,084 KB
testcase_07 AC 128 ms
55,792 KB
testcase_08 AC 128 ms
55,752 KB
testcase_09 AC 129 ms
55,904 KB
testcase_10 AC 130 ms
55,496 KB
testcase_11 AC 129 ms
56,108 KB
testcase_12 AC 133 ms
55,748 KB
testcase_13 AC 126 ms
55,772 KB
testcase_14 AC 127 ms
55,864 KB
testcase_15 AC 133 ms
55,728 KB
testcase_16 AC 131 ms
56,048 KB
testcase_17 AC 132 ms
56,028 KB
testcase_18 AC 124 ms
55,908 KB
testcase_19 AC 130 ms
55,748 KB
testcase_20 AC 125 ms
55,488 KB
testcase_21 AC 128 ms
55,872 KB
testcase_22 AC 126 ms
55,896 KB
testcase_23 AC 130 ms
56,072 KB
testcase_24 AC 130 ms
55,616 KB
testcase_25 AC 128 ms
55,888 KB
testcase_26 AC 142 ms
57,892 KB
testcase_27 AC 151 ms
55,704 KB
testcase_28 AC 159 ms
55,904 KB
testcase_29 AC 149 ms
55,688 KB
testcase_30 AC 158 ms
55,992 KB
testcase_31 AC 150 ms
55,656 KB
testcase_32 AC 178 ms
55,888 KB
testcase_33 AC 133 ms
55,668 KB
testcase_34 AC 134 ms
56,208 KB
testcase_35 AC 133 ms
55,924 KB
testcase_36 AC 132 ms
55,504 KB
testcase_37 AC 143 ms
55,876 KB
testcase_38 AC 159 ms
56,208 KB
testcase_39 AC 157 ms
55,720 KB
testcase_40 AC 160 ms
55,996 KB
testcase_41 AC 159 ms
55,988 KB
testcase_42 AC 158 ms
55,840 KB
testcase_43 AC 171 ms
55,944 KB
testcase_44 AC 131 ms
55,868 KB
testcase_45 AC 127 ms
55,868 KB
testcase_46 AC 130 ms
55,520 KB
testcase_47 AC 141 ms
55,972 KB
testcase_48 AC 148 ms
56,168 KB
testcase_49 AC 141 ms
55,884 KB
testcase_50 AC 157 ms
55,876 KB
testcase_51 AC 150 ms
56,300 KB
testcase_52 AC 157 ms
55,664 KB
testcase_53 AC 124 ms
55,916 KB
testcase_54 AC 130 ms
55,744 KB
testcase_55 AC 130 ms
55,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {
	
	public static boolean augment(boolean[][] adj, int node, int[] n_to_m, int[] m_to_n, boolean[] visited){
		final int n = adj.length;
		final int m = adj[0].length;
		
		if(node < 0){ return true; }
		for(int match = 0; match < m; match++){
			if(!adj[node][match]){ continue; }
			if(visited[match]){ continue; }
			
			visited[match] = true;
			if(augment(adj, m_to_n[match], n_to_m, m_to_n, visited)){
				n_to_m[node] = match;
				m_to_n[match] = node;
				return true;
			}
		}
		
		return false;
	}	
	
	public static int matching(boolean[][] adj){
		final int n = adj.length;
		final int m = adj[0].length;
		
		int[] n_to_m = new int[n];
		int[] m_to_n = new int[m];
		Arrays.fill(n_to_m, -1);
		Arrays.fill(m_to_n, -1);
		
		int match = 0;
		for(int node = 0; node < n; node++){
			boolean[] visited = new boolean[m];
			
			if(augment(adj, node, n_to_m, m_to_n, visited)){
				match++;
			}
		}
		
		return match;
	}
	
	public static final int toInt(int x, int y){
		return y * 120 + x;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int n = sc.nextInt();
		
		int[] f_rs = new int[n];
		int[] f_cs = new int[n];
		int[] s_rs = new int[n];
		int[] s_cs = new int[n];
		
		for(int i = 0; i < n; i++){
			final int f_r = sc.nextInt();
			final int f_c = sc.nextInt();
			final int s_r = sc.nextInt();
			final int s_c = sc.nextInt();
			
			f_rs[i] = f_r;
			f_cs[i] = f_c;
			s_rs[i] = s_r;
			s_cs[i] = s_c;
		}
		
		int points = 0;
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		for(int i = 0; i < n; i++){
			final int f_hash = toInt(f_rs[i], f_cs[i]);
			if(!map.containsKey(f_hash)){ map.put(f_hash, points++); }
			final int s_hash = toInt(s_rs[i], s_cs[i]);
			if(!map.containsKey(s_hash)){ map.put(s_hash, points++); }
		}
		
		boolean[][] adj = new boolean[n][points];
		for(int i = 0; i < n; i++){
			final int f_hash = toInt(f_rs[i], f_cs[i]);
			final int s_hash = toInt(s_rs[i], s_cs[i]);
			final int f_index = map.get(f_hash);
			final int s_index = map.get(s_hash);
			
			adj[i][f_index] = adj[i][s_index] = true;
		}
		
		System.out.println(matching(adj) == n ? "YES" : "NO");
	}
}
0