結果
問題 | No.2319 Friends+ |
ユーザー | kakel-san |
提出日時 | 2023-08-04 08:21:02 |
言語 | C# (.NET 8.0.203) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,156 bytes |
コンパイル時間 | 11,178 ms |
コンパイル使用メモリ | 170,296 KB |
実行使用メモリ | 242,732 KB |
最終ジャッジ日時 | 2024-10-14 05:26:13 |
合計ジャッジ時間 | 38,489 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 60 ms
30,720 KB |
testcase_01 | AC | 59 ms
30,592 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | AC | 446 ms
80,528 KB |
testcase_05 | WA | - |
testcase_06 | AC | 468 ms
80,652 KB |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | AC | 60 ms
30,464 KB |
testcase_18 | AC | 439 ms
78,668 KB |
testcase_19 | AC | 449 ms
80,468 KB |
testcase_20 | AC | 447 ms
79,892 KB |
testcase_21 | AC | 456 ms
80,036 KB |
testcase_22 | AC | 451 ms
79,928 KB |
testcase_23 | AC | 469 ms
80,052 KB |
testcase_24 | AC | 466 ms
80,052 KB |
testcase_25 | AC | 506 ms
79,676 KB |
testcase_26 | AC | 453 ms
79,892 KB |
testcase_27 | AC | 444 ms
79,852 KB |
testcase_28 | AC | 466 ms
79,708 KB |
testcase_29 | AC | 461 ms
80,068 KB |
testcase_30 | AC | 448 ms
79,804 KB |
testcase_31 | AC | 483 ms
80,024 KB |
testcase_32 | AC | 440 ms
79,744 KB |
testcase_33 | AC | 505 ms
79,648 KB |
testcase_34 | AC | 451 ms
79,392 KB |
testcase_35 | AC | 452 ms
79,592 KB |
testcase_36 | AC | 490 ms
81,268 KB |
testcase_37 | AC | 443 ms
80,532 KB |
testcase_38 | AC | 499 ms
80,512 KB |
testcase_39 | AC | 451 ms
80,484 KB |
testcase_40 | AC | 480 ms
80,732 KB |
testcase_41 | AC | 477 ms
80,640 KB |
testcase_42 | AC | 472 ms
80,272 KB |
testcase_43 | AC | 456 ms
80,644 KB |
testcase_44 | AC | 471 ms
80,620 KB |
testcase_45 | AC | 463 ms
80,248 KB |
testcase_46 | AC | 466 ms
242,732 KB |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (99 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[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); // Test(); } static void Test() { var r = new Random(); var count = 0; while (true) { var n = 100; var q = 500; var p = new int[n]; for (var i = 0; i < n; ++i) p[i] = r.Next(n) + 1; var (m, map) = Create(n, r); var query = new int[q][]; for (var i = 0; i < q; ++i) query[i] = new int[] { r.Next(n) + 1, r.Next(n) + 1 }; // 愚直解 var s1 = All(n, m, p, map, q, query); // 作成解 var s2 = FriendsPlus(n, m, p, map, q, query); if (s1 != s2) { WriteLine($"{n} {m}"); WriteLine(string.Join(" ", p)); WriteLine(string.Join("\n", map.Select(mi => string.Join(" ", mi)))); WriteLine(q); WriteLine(string.Join("\n", query.Select(qi => string.Join(" ", qi)))); WriteLine(s1); WriteLine(s2); return; } ++count; if (count % 1000 == 0) WriteLine(count); } } static (int m, int[][] map) Create(int n, Random r) { var list = new List<int[]>(); for (var i = 0; i < n; ++i) for (var j = i + 1; j < n; ++j) if (r.Next(3) == 0) { list.Add(new int[] { i + 1, j + 1 }); } return (list.Count, list.ToArray()); } static string All(int n, int m, int[] p, int[][] map, int q, int[][] query) { var fr = new UnionFindTree(n); foreach (var edge in map) fr.Unite(edge[0] - 1, edge[1] - 1); var pos = (int[]) p.Clone(); var ans = new bool[q]; for (var i = 0; i < q; ++i) { var x = query[i][0] - 1; var y = query[i][1] - 1; var flg = false; for (var f = 0; f < n; ++f) if (fr.IsSameTree(f, x)) { if (pos[f] == pos[y]) flg = true; } if (flg && pos[x] != pos[y]) { pos[x] = pos[y]; ans[i] = true; } } return string.Join("\n", ans.Select(f => f ? "Yes" : "No")); } static void Solve() { var c = NList; var (n, m) = (c[0], c[1]); var p = NList; var map = NArr(m); var q = NN; var query = NArr(q); WriteLine(FriendsPlus(n, m, p, map, q, query)); } static string FriendsPlus(int n, int m, int[] p, int[][] map, int q, int[][] query) { var fr = new UnionFindTree(n + q); foreach (var edge in map) fr.Unite(edge[0] - 1, edge[1] - 1); var gw = new Dictionary<int, int>[n + q]; for (var i = 0; i < gw.Length; ++i) gw[i] = new Dictionary<int, int>(); var pos = Enumerable.Repeat(-1, n + q).ToArray(); for (var i = 0; i < n; ++i) { pos[i] = p[i]; if (gw[fr.GetRoot(i)].ContainsKey(p[i])) ++gw[fr.GetRoot(i)][p[i]]; else gw[fr.GetRoot(i)][p[i]] = 1; } var id = new int[n]; for (var i = 0; i < n; ++i) id[i] = i; var ans = new bool[q]; for (var i = 0; i < q; ++i) { var x = query[i][0] - 1; var y = query[i][1] - 1; var gid = fr.GetRoot(x); var xgroup = gw[gid]; if (pos[x] != pos[y] && xgroup.ContainsKey(pos[y])) { ans[i] = true; --xgroup[pos[x]]; if (xgroup[pos[x]] == 0) xgroup.Remove(pos[x]); ++xgroup[pos[y]]; pos[x] = pos[y]; } } return string.Join("\n", ans.Select(f => f ? "Yes" : "No")); } class UnionFindTree { int[] roots; public UnionFindTree(int size) { roots = new int[size]; for (var i = 0; i < size; ++i) roots[i] = -1; } public int GetRoot(int a) { if (roots[a] < 0) return a; return roots[a] = GetRoot(roots[a]); } public bool IsSameTree(int a, int b) { return GetRoot(a) == GetRoot(b); } public bool Unite(int a, int b) { var x = GetRoot(a); var y = GetRoot(b); if (x == y) return false; if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; } roots[x] += roots[y]; roots[y] = x; return true; } public int GetSize(int a) { return -roots[GetRoot(a)]; } } }