結果
問題 | No.556 仁義なきサルたち |
ユーザー | bluemegane |
提出日時 | 2021-05-01 12:47:31 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 115 ms / 2,000 ms |
コード長 | 2,060 bytes |
コンパイル時間 | 1,580 ms |
コンパイル使用メモリ | 63,456 KB |
実行使用メモリ | 22,796 KB |
最終ジャッジ日時 | 2023-09-27 02:36:01 |
合計ジャッジ時間 | 5,252 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 62 ms
20,580 KB |
testcase_01 | AC | 62 ms
20,668 KB |
testcase_02 | AC | 62 ms
20,580 KB |
testcase_03 | AC | 63 ms
18,604 KB |
testcase_04 | AC | 62 ms
22,708 KB |
testcase_05 | AC | 63 ms
18,616 KB |
testcase_06 | AC | 63 ms
20,520 KB |
testcase_07 | AC | 66 ms
20,688 KB |
testcase_08 | AC | 64 ms
22,664 KB |
testcase_09 | AC | 68 ms
20,640 KB |
testcase_10 | AC | 68 ms
20,612 KB |
testcase_11 | AC | 68 ms
22,580 KB |
testcase_12 | AC | 68 ms
20,736 KB |
testcase_13 | AC | 84 ms
20,608 KB |
testcase_14 | AC | 87 ms
20,620 KB |
testcase_15 | AC | 87 ms
20,596 KB |
testcase_16 | AC | 109 ms
20,732 KB |
testcase_17 | AC | 112 ms
20,744 KB |
testcase_18 | AC | 113 ms
22,796 KB |
testcase_19 | AC | 112 ms
20,808 KB |
testcase_20 | AC | 113 ms
20,708 KB |
testcase_21 | AC | 115 ms
22,796 KB |
ソースコード
using System.Collections.Generic; 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 Dictionary<int, int>(); for (int i = 1; i <= n; i++) p[i] = i; 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)]; var bp = p[uf.Root(b)]; var tp = Min(ap, bp); p[uf.Root(a)] = tp; p[uf.Root(b)] = tp; } else { if (counta > countb) p[uf.Root(b)] = p[uf.Root(a)]; else p[uf.Root(a)] = p[uf.Root(b)]; } uf.Unite(a, b); } for (int i = 1; i <= n; i++) { Console.WriteLine(p[uf.Root(i)]); } } }