結果

問題 No.2316 Freight Train
ユーザー 👑 kakel-sankakel-san
提出日時 2023-08-03 21:57:31
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 396 ms / 2,000 ms
コード長 1,941 bytes
コンパイル時間 1,735 ms
コンパイル使用メモリ 114,364 KB
実行使用メモリ 41,472 KB
最終ジャッジ日時 2024-04-21 20:44:17
合計ジャッジ時間 14,236 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
19,072 KB
testcase_01 AC 33 ms
19,072 KB
testcase_02 AC 34 ms
18,816 KB
testcase_03 AC 379 ms
40,192 KB
testcase_04 AC 211 ms
38,016 KB
testcase_05 AC 172 ms
35,712 KB
testcase_06 AC 78 ms
25,472 KB
testcase_07 AC 294 ms
39,040 KB
testcase_08 AC 253 ms
41,472 KB
testcase_09 AC 290 ms
39,936 KB
testcase_10 AC 292 ms
39,680 KB
testcase_11 AC 288 ms
40,064 KB
testcase_12 AC 333 ms
38,528 KB
testcase_13 AC 387 ms
40,832 KB
testcase_14 AC 389 ms
40,704 KB
testcase_15 AC 394 ms
40,704 KB
testcase_16 AC 391 ms
40,832 KB
testcase_17 AC 387 ms
40,576 KB
testcase_18 AC 388 ms
40,960 KB
testcase_19 AC 392 ms
40,960 KB
testcase_20 AC 392 ms
40,704 KB
testcase_21 AC 396 ms
40,832 KB
testcase_22 AC 388 ms
40,960 KB
testcase_23 AC 346 ms
40,832 KB
testcase_24 AC 379 ms
40,832 KB
testcase_25 AC 351 ms
39,168 KB
testcase_26 AC 345 ms
38,528 KB
testcase_27 AC 238 ms
35,072 KB
testcase_28 AC 32 ms
19,328 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    static string[] SList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => ReadLine()).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, q) = (c[0], c[1]);
        var a = NList;
        var query = NArr(q);
        var uf = new UnionFindTree(n + 1);
        for (var i = 0; i < n; ++i)
        {
            if (a[i] > 0) uf.Unite(i + 1, a[i]);
        }
        var ans = new bool[q];
        for (var i = 0; i < q; ++i)
        {
            ans[i] = uf.IsSameTree(query[i][0], query[i][1]);
        }
        WriteLine(string.Join("\n", ans.Select(f => f ? "Yes" : "No")));
    }
    class UnionFindTree
    {
        int[] roots;
        public UnionFindTree(int size)
        {
            roots = new int[size];
            for (var i = 0; i < size; ++i) roots[i] = -1;
        }
        public int GetRoot(int a)
        {
            if (roots[a] < 0) return a;
            return roots[a] = GetRoot(roots[a]);
        }
        public bool IsSameTree(int a, int b)
        {
            return GetRoot(a) == GetRoot(b);
        }
        public bool Unite(int a, int b)
        {
            var x = GetRoot(a);
            var y = GetRoot(b);
            if (x == y) return false;
            if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; }
            roots[x] += roots[y];
            roots[y] = x;
            return true;
        }
        public int GetSize(int a)
        {
            return -roots[GetRoot(a)];
        }
    }
}
0