結果
問題 |
No.2914 正閉路検出
|
ユーザー |
|
提出日時 | 2024-10-04 22:39:23 |
言語 | C# (.NET 8.0.404) |
結果 |
AC
|
実行時間 | 468 ms / 2,000 ms |
コード長 | 3,642 bytes |
コンパイル時間 | 7,187 ms |
コンパイル使用メモリ | 166,996 KB |
実行使用メモリ | 227,232 KB |
最終ジャッジ日時 | 2024-10-04 22:39:51 |
合計ジャッジ時間 | 20,580 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 55 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (84 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/
ソースコード
namespace AtCoder; #nullable enable using System.Numerics; static class Extensions { public static T[] Repeat<T>(this int time, Func<T> F) => Enumerable.Range(0, time).Select(_ => F()).ToArray(); } class AtCoder { object? Solve() { var n = Int(); var m = Int(); var adjz = n.Repeat(() => new Dictionary<int, (int, int)>()); for (var i = 0; i < m; i++) { var u = Int() - 1; var v = Int() - 1; var w = Int(); if (adjz[u].TryGetValue(v, out var p)) { var (pw, pj) = p; if (pw == w) continue; Out(2); if (w > pw) { Out(v + 1); } else { Out(u + 1); } Out(pj + " " + (i + 1)); return null; } adjz[u][v] = (w, i + 1); adjz[v][u] = (-w, i + 1); } var dz = new long[n]; var last = n.Repeat(() => -1); var lj = n.Repeat(() => -1); var ans = new List<int>(); void S(int v, int prev) { if (ans.Count > 0) return; foreach (var (next, value) in adjz[v]) { var (w, j) = value; if (j == prev) continue; var nd = dz[v] + w; if (last[next] != -1) { if (dz[next] != nd && ans.Count == 0) { var k = v; ans.Add(j); while (k != next && k >= 0) { ans.Add(lj[k]); k = last[k]; } if (dz[next] < nd) ans.Reverse(); Out(ans.Count); Out(next + 1); Out(ans, " "); return; } continue; } dz[next] = nd; last[next] = v; lj[next] = j; S(next, j); } } for (var start = 0; start < n; start++) { if (last[start] != -1) continue; dz[start] = 0; last[start] = -2; S(start, -1); } if (ans.Count == 0) return -1; return null; } public static void Main() => new AtCoder().Run(); public void Run() { var res = Solve(); if (res != null) { if (res is bool yes) res = yes ? "Yes" : "No"; sw.WriteLine(res); } sw.Flush(); } string[] input = Array.Empty<string>(); int iter = 0; readonly StreamWriter sw = new(Console.OpenStandardOutput()) { AutoFlush = false }; string String() { while (iter >= input.Length) (input, iter) = (Console.ReadLine()!.Trim().Split(' '), 0); return input[iter++]; } T Input<T>() where T : IParsable<T> => T.Parse(String(), null); int Int() => Input<int>(); void Out(object? x, string? separator = null) { separator ??= Environment.NewLine; if (x is System.Collections.IEnumerable obj and not string) { var firstLine = true; foreach (var item in obj) { if (!firstLine) sw.Write(separator); firstLine = false; sw.Write(item); } } else sw.Write(x); sw.WriteLine(); } }