結果

問題 No.3650 Teleportation Cycles
コンテスト
ユーザー tobisatis
提出日時 2026-08-28 21:32:07
言語 C#
(.NET 10.0.201)
コンパイル:
dotnet_c
実行:
/usr/bin/dotnet_wrap
結果
AC  
実行時間 75 ms / 2,000 ms
+ 534µs
コード長 1,219 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 13,119 ms
コンパイル使用メモリ 171,260 KB
実行使用メモリ 218,860 KB
最終ジャッジ日時 2026-08-28 21:32:24
合計ジャッジ時間 10,435 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (87 ミリ秒)。
  main -> /home/judge/data/code/bin/Release/net10.0/main.dll
  main -> /home/judge/data/code/bin/Release/net10.0/publish/

ソースコード

diff #
raw source code

#nullable enable

#region
var (_input, _iter) = (Array.Empty<string>(), 0);
T I<T>() where T : IParsable<T>
{
    while (_iter >= _input.Length) (_input, _iter) = (Console.ReadLine()!.Trim().Split(' '), 0);
    return T.Parse(_input[_iter++], null);
}
#endregion

static T[] Range<T>(int n, Func<T> F) => Enumerable.Range(0, n).Select(_ => F()).ToArray();

var n = I<int>();
var az = Range(n, () => I<int>() - 1);

var uf = new UnionFind(n);
for (var i = 0; i < n; i++) uf.Union(i, az[i]);

var ans = 0;
for (var i = 0; i < n; i++) if (uf.Find(i) == i) ans++;
Console.WriteLine(ans);

class UnionFind
{
    int[] Verticals { get; set; }
    public UnionFind(int verticals)
    {
        Verticals = new int[verticals];
        Verticals.AsSpan().Fill(-1);
    }

    public int Union(int x, int y)
    {
        (x, y) = (Find(x), Find(y));
        if (x == y) return x;
        var (sx, sy) = (Size(x), Size(y));
        if (sx < sy) (x, y, sy) = (y, x, sx);
        Verticals[x] -= sy;
        return Verticals[y] = x;
    }

    public int Find(int x)
    {
        var p = Verticals[x];
        if (p < 0) return x;
        return Verticals[x] = Find(p);
    }

    public int Size(int x) => -Verticals[Find(x)];
}
0