結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー 👑 kakel-sankakel-san
提出日時 2023-08-04 23:04:29
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 260 ms / 2,000 ms
コード長 2,206 bytes
コンパイル時間 1,303 ms
コンパイル使用メモリ 64,308 KB
実行使用メモリ 34,564 KB
最終ジャッジ日時 2023-08-17 18:40:39
合計ジャッジ時間 6,699 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
23,952 KB
testcase_01 AC 64 ms
21,828 KB
testcase_02 AC 64 ms
21,764 KB
testcase_03 AC 64 ms
21,888 KB
testcase_04 AC 154 ms
29,056 KB
testcase_05 AC 233 ms
31,128 KB
testcase_06 AC 250 ms
31,924 KB
testcase_07 AC 178 ms
32,784 KB
testcase_08 AC 192 ms
31,540 KB
testcase_09 AC 260 ms
34,564 KB
testcase_10 AC 252 ms
34,088 KB
testcase_11 AC 204 ms
32,340 KB
testcase_12 AC 256 ms
32,692 KB
testcase_13 AC 194 ms
31,460 KB
testcase_14 AC 65 ms
21,904 KB
testcase_15 AC 64 ms
21,952 KB
testcase_16 AC 64 ms
23,892 KB
testcase_17 AC 64 ms
21,900 KB
testcase_18 AC 64 ms
23,896 KB
testcase_19 AC 68 ms
23,972 KB
testcase_20 AC 65 ms
23,860 KB
testcase_21 AC 65 ms
22,608 KB
testcase_22 AC 68 ms
23,672 KB
testcase_23 AC 68 ms
23,592 KB
testcase_24 AC 68 ms
23,232 KB
testcase_25 AC 66 ms
23,096 KB
testcase_26 AC 64 ms
19,808 KB
testcase_27 AC 72 ms
23,836 KB
testcase_28 AC 71 ms
23,292 KB
testcase_29 AC 67 ms
21,856 KB
testcase_30 AC 71 ms
22,388 KB
testcase_31 AC 73 ms
23,820 KB
testcase_32 AC 68 ms
21,840 KB
testcase_33 AC 68 ms
24,076 KB
testcase_34 AC 69 ms
21,708 KB
権限があれば一括ダウンロードができます

ソースコード

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 from = new int[n];
        var to = new int[n];
        var uf = new UnionFindTree(n);
        foreach (var edge in map)
        {
            ++from[edge[0]];
            ++to[edge[1]];
            uf.Unite(edge[0], edge[1]);
        }
        var pts = new int[n];
        for (var i = 0; i < n; ++i) if (from[i] > to[i]) pts[uf.GetRoot(i)] += from[i] - to[i];
        var list = new List<int>();
        for (var i = 0; i < n; ++i) if (uf.GetRoot(i) == i && (uf.GetSize(i) > 1 || from[i] > 0))
        {
            list.Add(pts[i]);
        }
        var ans = list.Count - 1;
        foreach (var li in list) ans += Math.Max(0, li - 1);
        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