結果
問題 | No.556 仁義なきサルたち |
ユーザー |
![]() |
提出日時 | 2023-01-10 22:31:06 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 9 ms / 2,000 ms |
コード長 | 1,523 bytes |
コンパイル時間 | 2,164 ms |
コンパイル使用メモリ | 206,000 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-06-22 17:17:18 |
合計ジャッジ時間 | 3,294 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
import std;void main() {int N, M;readf("%d %d\n", N, M);auto uf = new UnionFind!int(N+1);foreach (_; 0 .. M) {int A, B;readf("%d %d\n", A, B);uf.unite(A, B);}foreach (i; 1 .. N+1) {uf.root(i).writeln;}}/// Union-Findstruct UnionFind(T)if (isIntegral!T) {/// Constructorthis(T n) nothrow @safe {len = n;par.length = len;cnt.length = len;foreach (i; 0 .. len) {par[i] = i;}cnt[] = 1;}/// Returns the root of x.T root(T x) nothrow @nogc @safein (0 <= x && x < len) {if (par[x] == x) {return x;}else {return par[x] = root(par[x]);}}/// Returns whether x and y have the same root.bool isSame(T x, T y) nothrow @nogc @safein (0 <= x && x < len && 0 <= y && y < len) {return root(x) == root(y);}/// Unites x tree and y tree.void unite(T x, T y) nothrow @nogc @safein (0 <= x && x < len && 0 <= y && y < len) {x = root(x), y = root(y);if (x == y) {return;}if (cnt[x] > cnt[y] || (cnt[x] == cnt[y] && x < y)) {swap(x, y);}cnt[y] += cnt[x];par[x] = y;}/// Returns the size of the x tree.T size(T x) nothrow @nogc @safein (0 <= x && x < len) {return cnt[root(x)];}//private:T len;T[] par;T[] cnt;}