結果

問題 No.1639 最小通信路
ユーザー 👑 kakel-sankakel-san
提出日時 2021-08-06 22:10:57
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 2,282 bytes
コンパイル時間 2,762 ms
コンパイル使用メモリ 112,516 KB
実行使用メモリ 27,048 KB
最終ジャッジ日時 2023-10-17 03:33:53
合計ジャッジ時間 6,081 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
24,152 KB
testcase_01 AC 33 ms
25,000 KB
testcase_02 AC 85 ms
25,000 KB
testcase_03 AC 91 ms
27,048 KB
testcase_04 AC 94 ms
27,048 KB
testcase_05 AC 40 ms
25,000 KB
testcase_06 AC 49 ms
25,000 KB
testcase_07 AC 30 ms
25,000 KB
testcase_08 AC 65 ms
25,000 KB
testcase_09 AC 31 ms
25,000 KB
testcase_10 AC 35 ms
25,000 KB
testcase_11 AC 56 ms
25,000 KB
testcase_12 AC 36 ms
25,000 KB
testcase_13 AC 81 ms
25,000 KB
testcase_14 AC 29 ms
25,000 KB
testcase_15 AC 35 ms
25,000 KB
testcase_16 AC 57 ms
25,000 KB
testcase_17 AC 67 ms
25,000 KB
testcase_18 AC 32 ms
25,000 KB
testcase_19 AC 43 ms
25,000 KB
testcase_20 AC 67 ms
25,000 KB
testcase_21 AC 30 ms
25,000 KB
testcase_22 AC 37 ms
25,000 KB
testcase_23 AC 47 ms
25,000 KB
testcase_24 AC 36 ms
25,000 KB
testcase_25 AC 33 ms
25,004 KB
testcase_26 AC 31 ms
25,000 KB
testcase_27 AC 30 ms
25,000 KB
testcase_28 AC 30 ms
25,000 KB
testcase_29 AC 44 ms
25,000 KB
testcase_30 AC 57 ms
25,000 KB
testcase_31 AC 33 ms
25,000 KB
testcase_32 AC 47 ms
25,000 KB
testcase_33 AC 32 ms
25,000 KB
testcase_34 AC 46 ms
25,000 KB
testcase_35 AC 73 ms
27,048 KB
testcase_36 AC 45 ms
25,000 KB
testcase_37 AC 49 ms
25,000 KB
testcase_38 AC 30 ms
25,000 KB
testcase_39 AC 33 ms
25,000 KB
testcase_40 AC 30 ms
25,004 KB
testcase_41 AC 32 ms
25,000 KB
testcase_42 AC 53 ms
25,000 KB
testcase_43 AC 32 ms
25,000 KB
testcase_44 AC 30 ms
25,000 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 System.Collections.Generic;
using static System.Console;
using System.Linq;

class yuki308
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static long[] LList => ReadLine().Split().Select(long.Parse).ToArray();
    static string[] SList(int n) => Enumerable.Repeat(0, n).Select(_ => ReadLine()).ToArray();
    static void Main()
    {
        var n = NN;
        var map = new Pair[n * (n - 1) / 2];
        for (var i = 0; i < map.Length; ++i)
        {
            var c = ReadLine().Split();
            map[i] = new Pair(int.Parse(c[0]) - 1, int.Parse(c[1]) - 1, c[2]);
        }
        Array.Sort(map);
        var uf = new UnionFindTree(n);
        var r = "";
        foreach (var way in map)
        {
            if (!uf.IsSameTree(way.TA, way.TB))
            {
                uf.Unite(way.TA, way.TB);
                r = way.Str;
            }
        }
        WriteLine(r);
    }
    class Pair : IComparable<Pair>
    {
        public int TA;
        public int TB;
        public string Str;
        public Pair(int a, int b, string str)
        {
            TA = a; TB = b; Str = str;
        }
        public int CompareTo(Pair b)
        {
            var c = Str.Length.CompareTo(b.Str.Length);
            if (c != 0) return c;
            return Str.CompareTo(b.Str);
        }
    }

    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