結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー 👑 kakel-sankakel-san
提出日時 2023-08-20 15:14:12
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 136 ms / 2,000 ms
コード長 1,789 bytes
コンパイル時間 5,940 ms
コンパイル使用メモリ 112,900 KB
実行使用メモリ 27,136 KB
最終ジャッジ日時 2024-05-07 21:40:10
合計ジャッジ時間 8,591 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
19,328 KB
testcase_01 AC 28 ms
19,328 KB
testcase_02 AC 29 ms
19,072 KB
testcase_03 AC 122 ms
27,008 KB
testcase_04 AC 41 ms
21,612 KB
testcase_05 AC 102 ms
25,216 KB
testcase_06 AC 88 ms
25,344 KB
testcase_07 AC 106 ms
25,728 KB
testcase_08 AC 87 ms
25,600 KB
testcase_09 AC 117 ms
26,752 KB
testcase_10 AC 127 ms
27,008 KB
testcase_11 AC 109 ms
26,368 KB
testcase_12 AC 85 ms
25,472 KB
testcase_13 AC 55 ms
23,296 KB
testcase_14 AC 76 ms
24,576 KB
testcase_15 AC 106 ms
25,728 KB
testcase_16 AC 136 ms
27,136 KB
testcase_17 AC 67 ms
24,064 KB
testcase_18 AC 78 ms
24,704 KB
testcase_19 AC 34 ms
20,392 KB
testcase_20 AC 115 ms
25,856 KB
testcase_21 AC 66 ms
23,424 KB
testcase_22 AC 75 ms
24,064 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[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
    static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, m) = (c[0], c[1]);
        var map = NMap(m);
        var uf = new UnionFindTree(n * 2);
        foreach (var edge in map)
        {
            uf.Unite(edge[0], edge[1]);
        }
        var ans = n;
        for (var i = 0; i < n * 2; ++i) if (uf.GetRoot(i) == i) ans -= uf.GetSize(i) / 2;
        WriteLine(ans);
    }
    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