結果

問題 No.2200 Weird Shortest Path
ユーザー 👑 kakel-sankakel-san
提出日時 2023-01-28 20:36:03
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 441 ms / 2,000 ms
コード長 1,765 bytes
コンパイル時間 1,990 ms
コンパイル使用メモリ 67,736 KB
実行使用メモリ 38,984 KB
最終ジャッジ日時 2023-09-11 08:02:20
合計ジャッジ時間 16,232 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
23,732 KB
testcase_01 AC 63 ms
21,712 KB
testcase_02 AC 64 ms
21,812 KB
testcase_03 AC 66 ms
23,712 KB
testcase_04 AC 67 ms
21,872 KB
testcase_05 AC 64 ms
21,780 KB
testcase_06 AC 65 ms
21,804 KB
testcase_07 AC 66 ms
23,808 KB
testcase_08 AC 67 ms
23,992 KB
testcase_09 AC 66 ms
19,708 KB
testcase_10 AC 64 ms
21,868 KB
testcase_11 AC 65 ms
21,656 KB
testcase_12 AC 66 ms
23,860 KB
testcase_13 AC 64 ms
21,796 KB
testcase_14 AC 66 ms
21,912 KB
testcase_15 AC 68 ms
23,872 KB
testcase_16 AC 64 ms
21,764 KB
testcase_17 AC 177 ms
31,196 KB
testcase_18 AC 264 ms
33,492 KB
testcase_19 AC 433 ms
36,460 KB
testcase_20 AC 413 ms
37,776 KB
testcase_21 AC 194 ms
31,476 KB
testcase_22 AC 250 ms
32,796 KB
testcase_23 AC 439 ms
38,564 KB
testcase_24 AC 125 ms
27,080 KB
testcase_25 AC 422 ms
36,428 KB
testcase_26 AC 435 ms
36,564 KB
testcase_27 AC 187 ms
31,396 KB
testcase_28 AC 332 ms
35,680 KB
testcase_29 AC 422 ms
38,540 KB
testcase_30 AC 427 ms
38,492 KB
testcase_31 AC 280 ms
32,092 KB
testcase_32 AC 436 ms
36,568 KB
testcase_33 AC 437 ms
38,672 KB
testcase_34 AC 441 ms
36,660 KB
testcase_35 AC 439 ms
38,716 KB
testcase_36 AC 440 ms
36,756 KB
testcase_37 AC 440 ms
36,868 KB
testcase_38 AC 439 ms
38,756 KB
testcase_39 AC 247 ms
32,992 KB
testcase_40 AC 369 ms
38,984 KB
testcase_41 AC 236 ms
33,128 KB
testcase_42 AC 360 ms
38,588 KB
testcase_43 AC 397 ms
38,652 KB
testcase_44 AC 398 ms
36,736 KB
testcase_45 AC 399 ms
36,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
class Program
{
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, m) = (c[0], c[1]);
        var map = NArr(m);
        Array.Sort(map, (l, r) => l[2].CompareTo(r[2]));
        var uf = new UnionFindTree(n);
        var ans = 0L;
        foreach (var edge in map)
        {
            var a = edge[0] - 1;
            var b = edge[1] - 1;
            if (!uf.IsSameTree(a, b))
            {
                ans += (long)edge[2] * uf.GetSize(a) * uf.GetSize(b);
                uf.Unite(a, b);
            }
        }
        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