結果
問題 | No.1640 簡単な色塗り |
ユーザー |
|
提出日時 | 2021-11-29 15:06:32 |
言語 | Java (openjdk 23) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 1,459 bytes |
コンパイル時間 | 3,574 ms |
コンパイル使用メモリ | 78,956 KB |
実行使用メモリ | 158,260 KB |
最終ジャッジ日時 | 2024-07-02 11:50:21 |
合計ジャッジ時間 | 78,446 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 45 TLE * 8 |
ソースコード
import java.util.Scanner; import java.util.List; import java.util.ArrayList; import java.util.Arrays; public class No1640 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); N = Integer.valueOf(scan.nextLine()); G = new ArrayList<List<Integer>>(); for (int i=0; i < 2*N; i++) { G.add(new ArrayList<Integer>()); } match = new int[2*N]; Arrays.fill(match, -1); used = new boolean[2*N]; for (int i=0; i < N; i++) { String[] s = scan.nextLine().split(" "); add_edge(i, Integer.valueOf(s[0]) + N - 1); add_edge(i, Integer.valueOf(s[1]) + N - 1); } int n = bipartite_matching(); if (n == N) { System.out.println("Yes"); for (int i=0; i < N; i++) { System.out.println(match[i] - N + 1); } } else { System.out.println("No"); } } private static int N; private static List<List<Integer>> G; private static int[] match; private static boolean[] used; private static void add_edge(int u, int v) { G.get(u).add(v); G.get(v).add(u); } private static boolean dfs(int v) { used[v] = true; for (int u : G.get(v)) { int w = match[u]; if (w < 0 || (!used[w] && dfs(w))) { match[v] = u; match[u] = v; return true; } } return false; } private static int bipartite_matching() { int res = 0; for (int v=0; v < 2*N; v++) { if (match[v] < 0) { Arrays.fill(used, false); if (dfs(v)) { res++; } } } return res; } }