結果
問題 | No.556 仁義なきサルたち |
ユーザー | bluemegane |
提出日時 | 2021-05-01 12:55:30 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 71 ms / 2,000 ms |
コード長 | 2,078 bytes |
コンパイル時間 | 1,779 ms |
コンパイル使用メモリ | 63,432 KB |
実行使用メモリ | 22,876 KB |
最終ジャッジ日時 | 2023-09-27 02:46:18 |
合計ジャッジ時間 | 3,736 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 60 ms
20,644 KB |
testcase_01 | AC | 60 ms
20,792 KB |
testcase_02 | AC | 59 ms
20,736 KB |
testcase_03 | AC | 60 ms
22,752 KB |
testcase_04 | AC | 60 ms
20,824 KB |
testcase_05 | AC | 59 ms
20,712 KB |
testcase_06 | AC | 60 ms
20,656 KB |
testcase_07 | AC | 60 ms
20,908 KB |
testcase_08 | AC | 60 ms
20,684 KB |
testcase_09 | AC | 60 ms
20,828 KB |
testcase_10 | AC | 60 ms
20,720 KB |
testcase_11 | AC | 60 ms
20,792 KB |
testcase_12 | AC | 61 ms
22,744 KB |
testcase_13 | AC | 61 ms
20,732 KB |
testcase_14 | AC | 62 ms
20,616 KB |
testcase_15 | AC | 65 ms
20,840 KB |
testcase_16 | AC | 64 ms
22,876 KB |
testcase_17 | AC | 66 ms
20,688 KB |
testcase_18 | AC | 69 ms
20,680 KB |
testcase_19 | AC | 69 ms
22,776 KB |
testcase_20 | AC | 70 ms
20,708 KB |
testcase_21 | AC | 71 ms
22,820 KB |
ソースコード
using static System.Math; using System; public class UnionFind { private int[] data; public UnionFind(int size) { data = new int[size]; for (int i = 0; i < size; i++) data[i] = -1; } public bool Unite(int x, int y) { x = Root(x); y = Root(y); if (x != y) { if (data[y] < data[x]) { var tmp = y; y = x; x = tmp; } data[x] += data[y]; data[y] = x; } return x != y; } public bool IsSameGroup(int x, int y) => Root(x) == Root(y); public int Root(int x) => data[x] < 0 ? x : data[x] = Root(data[x]); public int getMem(int x) => -data[Root(x)]; } public class Hello { static void Main() { string[] line = Console.ReadLine().Trim().Split(' '); var n = int.Parse(line[0]); var m = int.Parse(line[1]); getAns(n, m); } static void getAns(int n, int m) { var uf = new UnionFind(n + 1); var p = new int[n]; for (int i = 0; i < n; i++) p[i] = i + 1; for (int i = 0; i < m; i++) { string[] line = Console.ReadLine().Trim().Split(' '); var a = int.Parse(line[0]); var b = int.Parse(line[1]); if (uf.IsSameGroup(a, b)) continue; var counta = uf.getMem(a); var countb = uf.getMem(b); if (counta == countb) { var ap = p[uf.Root(a)-1]; var bp = p[uf.Root(b) -1]; var tp = Min(ap, bp); p[uf.Root(a)-1 ] = tp; p[uf.Root(b) -1] = tp; } else { if (counta > countb) p[uf.Root(b) -1] = p[uf.Root(a)-1]; else p[uf.Root(a) -1] = p[uf.Root(b)-1]; } uf.Unite(a, b); } var ans = new int[n]; for (int i = 1; i <= n; i++) ans[i - 1] = p[uf.Root(i) - 1]; Console.WriteLine(string.Join("\n",ans)); } }