結果
問題 | No.1813 Magical Stones |
ユーザー | kakel-san |
提出日時 | 2023-12-22 21:35:37 |
言語 | C# (.NET 8.0.203) |
結果 |
AC
|
実行時間 | 269 ms / 2,000 ms |
コード長 | 4,245 bytes |
コンパイル時間 | 8,294 ms |
コンパイル使用メモリ | 166,324 KB |
実行使用メモリ | 192,768 KB |
最終ジャッジ日時 | 2024-09-27 11:29:49 |
合計ジャッジ時間 | 17,111 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 55 ms
30,464 KB |
testcase_01 | AC | 55 ms
30,592 KB |
testcase_02 | AC | 64 ms
30,456 KB |
testcase_03 | AC | 88 ms
43,896 KB |
testcase_04 | AC | 56 ms
30,592 KB |
testcase_05 | AC | 55 ms
30,592 KB |
testcase_06 | AC | 60 ms
31,616 KB |
testcase_07 | AC | 58 ms
30,464 KB |
testcase_08 | AC | 60 ms
30,592 KB |
testcase_09 | AC | 59 ms
30,720 KB |
testcase_10 | AC | 58 ms
30,592 KB |
testcase_11 | AC | 93 ms
41,856 KB |
testcase_12 | AC | 56 ms
30,464 KB |
testcase_13 | AC | 57 ms
30,464 KB |
testcase_14 | AC | 89 ms
43,884 KB |
testcase_15 | AC | 257 ms
75,396 KB |
testcase_16 | AC | 262 ms
75,392 KB |
testcase_17 | AC | 260 ms
79,620 KB |
testcase_18 | AC | 262 ms
75,396 KB |
testcase_19 | AC | 259 ms
75,652 KB |
testcase_20 | AC | 259 ms
75,656 KB |
testcase_21 | AC | 254 ms
75,396 KB |
testcase_22 | AC | 252 ms
75,524 KB |
testcase_23 | AC | 255 ms
75,388 KB |
testcase_24 | AC | 57 ms
31,104 KB |
testcase_25 | AC | 83 ms
38,260 KB |
testcase_26 | AC | 67 ms
33,664 KB |
testcase_27 | AC | 201 ms
69,640 KB |
testcase_28 | AC | 269 ms
74,740 KB |
testcase_29 | AC | 236 ms
73,176 KB |
testcase_30 | AC | 234 ms
72,356 KB |
testcase_31 | AC | 251 ms
74,172 KB |
testcase_32 | AC | 58 ms
30,464 KB |
testcase_33 | AC | 56 ms
30,592 KB |
testcase_34 | AC | 64 ms
32,768 KB |
testcase_35 | AC | 86 ms
41,216 KB |
testcase_36 | AC | 62 ms
31,616 KB |
testcase_37 | AC | 131 ms
51,840 KB |
testcase_38 | AC | 91 ms
40,576 KB |
testcase_39 | AC | 199 ms
68,656 KB |
testcase_40 | AC | 62 ms
31,872 KB |
testcase_41 | AC | 66 ms
33,792 KB |
testcase_42 | AC | 86 ms
41,984 KB |
testcase_43 | AC | 77 ms
192,768 KB |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (90 ms)。 MSBuild のバージョン 17.9.6+a4ecab324 (.NET) main -> /home/judge/data/code/bin/Release/net8.0/main.dll main -> /home/judge/data/code/bin/Release/net8.0/publish/
ソースコード
using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray(); static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, m) = (c[0], c[1]); var map = NMap(m); var g = new SCCGraph(n); foreach (var edge in map) g.AddEdge(edge[0], edge[1]); var scc = g.SCC(); if (scc.Length == 1) { WriteLine(0); return; } var group = new int[n]; for (var i = 0; i < scc.Length; ++i) { foreach (var pos in scc[i]) group[pos] = i; } var osc = new int[scc.Length]; var isc = new int[scc.Length]; foreach (var edge in map) { if (group[edge[0]] != group[edge[1]]) { ++osc[group[edge[0]]]; ++isc[group[edge[1]]]; } } WriteLine(Math.Max(osc.Count(i => i == 0), isc.Count(i => i == 0))); } class SCCGraph { private int _n; private List<(int from, int to)> edges; public SCCGraph(int n) { _n = n; edges = new List<(int, int)>(); } public int NumVertices() { return _n; } public void AddEdge(int from, int to) { edges.Add((from, to )); } public (int, int[]) SCCIds() { var g = new CSR<int>(_n, edges); var nowOrd = 0; var groupNum = 0; var visited = new List<int>(_n); var low = new int[_n]; var ord = Enumerable.Repeat(-1, _n).ToArray(); var ids = new int[_n]; void DFS(int v) { low[v] = ord[v] = nowOrd++; visited.Add(v); for (var i = g.Start[v]; i < g.Start[v + 1]; ++i) { var to = g.EList[i]; if (ord[to] == -1) { DFS(to); low[v] = Math.Min(low[v], low[to]); } else low[v] = Math.Min(low[v], ord[to]); } if (low[v] == ord[v]) { while (true) { var u = visited.Last(); visited.RemoveAt(visited.Count - 1); ord[u] = _n; ids[u] = groupNum; if (u == v) break; } ++groupNum; } } for (var i = 0; i < _n; ++i) if (ord[i] == -1) DFS(i); for (var i = 0; i < ids.Length; ++i) ids[i] = groupNum - 1 - ids[i]; return (groupNum, ids); } public List<int>[] SCC() { var (groupNum, list) = SCCIds(); var counts = new int[groupNum]; foreach (var x in list) ++counts[x]; var groups = new List<int>[groupNum]; for (var i = 0; i < groups.Length; ++i) groups[i] = new List<int>(counts[i]); for (var i = 0; i < _n; ++i) groups[list[i]].Add(i); return groups; } } // 有向辺のリスト class CSR<E> { public int[] Start { get; private set; } public E[] EList { get; private set; } public CSR(int n, List<(int, E)> edges) { Start = new int[n + 1]; EList = new E[edges.Count]; foreach (var e in edges) ++Start[e.Item1 + 1]; for (var i = 1; i <= n; ++i) Start[i] += Start[i - 1]; var counter = (int[])Start.Clone(); foreach (var e in edges) { EList[counter[e.Item1]++] = e.Item2; } } } }