Please sign in first.
Submission #24579123
Source Code Expand
import java.util.*; // C - 3 Steps // 解説PDFを読んでから実装 public class Main { static List<List<Integer>> graph; // グラフ static int[] color; // 点の色 public static void main(String[] args) { // 入力と初期化 Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); // 点の数 int m = Integer.parseInt(sc.next()); // 辺の数 graph = new ArrayList<List<Integer>>(); for (int v = 0; v < n; v++) { graph.add(new ArrayList<Integer>()); } for (int e = 0; e < m; e++) { int from = Integer.parseInt(sc.next()) - 1; // 始点 int to = Integer.parseInt(sc.next()) - 1; // 終点 graph.get(from).add(to); graph.get(to).add(from); // 無向グラフであれば逆向きもある } color = new int[n]; Arrays.fill(color, 0); // 二部グラフかどうか判定 boolean nibu = checkNibu(0); // 確認用 // System.out.println("NO " + Arrays.toString(color)); // pairs := 点と点のペアのうち辺でつながることができるものの数 // 二部グラフの場合、白い点と黒い点のペアだけ // 二部グラフではない場合、すべてのペア long pairs = 0; if (nibu) { for (int v = 0; v < n; v++) { pairs += (color[v] == 1) ? 1 : 0; } pairs *= (long) n - pairs; } else { pairs = (long) n * ((long) n - 1) / 2; } // pairs箇所のうちm箇所はもう辺があるのでそれを引き算すれば答えが出る System.out.println(pairs - (long) m); } // 二部グラフかどうかをBFSの要領で判定 static boolean checkNibu(int s) { boolean ret = true; // スタート地点をキューに入れる Queue<Integer> queue = new ArrayDeque<Integer>(); queue.add(s); color[s] = 1; // キューが空になるまでループ while (!queue.isEmpty()) { // キューからひとつ取り出す int v = queue.poll(); // 次の点それぞれ処理 for (int next : graph.get(v)) { if (color[next] == 0) { // 無色なら色を塗ってキューに入れる color[next] = -color[v]; queue.add(next); } else { // 色があるなら二部グラフに反していないか確認 if (color[v] == color[next]) { ret = false; } } } } return ret; } }
Submission Info
Submission Time | |
---|---|
Task | C - 3 Steps |
User | tobi00604 |
Language | Java (OpenJDK 11.0.6) |
Score | 500 |
Code Size | 2379 Byte |
Status | AC |
Exec Time | 550 ms |
Memory | 72108 KiB |
Judge Result
Set Name | sample | all | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 500 / 500 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
sample | sample-01.txt, sample-02.txt |
all | sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, 02-14.txt, sample-01.txt, sample-02.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
01-01.txt | AC | 117 ms | 35568 KiB |
01-02.txt | AC | 113 ms | 35592 KiB |
01-03.txt | AC | 113 ms | 35496 KiB |
01-04.txt | AC | 111 ms | 35620 KiB |
01-05.txt | AC | 112 ms | 35808 KiB |
01-06.txt | AC | 140 ms | 38232 KiB |
01-07.txt | AC | 113 ms | 35984 KiB |
01-08.txt | AC | 141 ms | 37840 KiB |
01-09.txt | AC | 154 ms | 39636 KiB |
01-10.txt | AC | 163 ms | 41700 KiB |
02-01.txt | AC | 481 ms | 70388 KiB |
02-02.txt | AC | 527 ms | 71868 KiB |
02-03.txt | AC | 534 ms | 71348 KiB |
02-04.txt | AC | 538 ms | 72056 KiB |
02-05.txt | AC | 544 ms | 71948 KiB |
02-06.txt | AC | 532 ms | 71592 KiB |
02-07.txt | AC | 467 ms | 67060 KiB |
02-08.txt | AC | 530 ms | 71444 KiB |
02-09.txt | AC | 550 ms | 72108 KiB |
02-10.txt | AC | 383 ms | 61156 KiB |
02-11.txt | AC | 430 ms | 63484 KiB |
02-12.txt | AC | 479 ms | 67084 KiB |
02-13.txt | AC | 484 ms | 70920 KiB |
02-14.txt | AC | 516 ms | 71776 KiB |
sample-01.txt | AC | 103 ms | 35492 KiB |
sample-02.txt | AC | 99 ms | 35676 KiB |