結果

問題 No.583 鉄道同好会
ユーザー kokatsu
提出日時 2023-01-09 21:58:43
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 1,722 bytes
コンパイル時間 2,547 ms
コンパイル使用メモリ 206,184 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-22 17:17:14
合計ジャッジ時間 4,027 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

import std;
void main() {
int N, M;
readf("%d %d\n", N, M);
auto uf = new UnionFind!int(N);
auto cnts = new int[](N);
foreach (_; 0 .. M) {
int S, T;
readf("%d %d\n", S, T);
uf.unite(S, T);
++cnts[S], ++cnts[T];
}
int pos = -1, cnt1, cnt2;
foreach (i; 0 .. N) {
if (cnts[i] == 0) continue;
if (uf.root(i) == i) pos = i, ++cnt1;
if (cnts[i] % 2 == 1) ++cnt2;
}
writeln(cnt1 == 1 && cnt2 <= 2 ? "YES" : "NO");
}
/// Union-Find
struct UnionFind(T)
if (isIntegral!T) {
/// Constructor
this(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 @safe
in (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 @safe
in (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 @safe
in (0 <= x && x < len && 0 <= y && y < len) {
x = root(x), y = root(y);
if (x == y) {
return;
}
if (cnt[x] > cnt[y]) {
swap(x, y);
}
cnt[y] += cnt[x];
par[x] = y;
}
/// Returns the size of the x tree.
T size(T x) nothrow @nogc @safe
in (0 <= x && x < len) {
return cnt[root(x)];
}
private:
T len;
T[] par;
T[] cnt;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0