結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー 👑 kakel-sankakel-san
提出日時 2024-02-16 22:32:53
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 669 ms / 2,500 ms
コード長 2,571 bytes
コンパイル時間 8,037 ms
コンパイル使用メモリ 158,512 KB
実行使用メモリ 206,608 KB
最終ジャッジ日時 2024-02-16 22:33:43
合計ジャッジ時間 20,262 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 285 ms
45,220 KB
testcase_01 AC 632 ms
58,276 KB
testcase_02 AC 546 ms
58,916 KB
testcase_03 AC 96 ms
33,572 KB
testcase_04 AC 116 ms
33,700 KB
testcase_05 AC 90 ms
33,572 KB
testcase_06 AC 307 ms
42,660 KB
testcase_07 AC 481 ms
58,916 KB
testcase_08 AC 526 ms
58,788 KB
testcase_09 AC 503 ms
59,044 KB
testcase_10 AC 669 ms
58,916 KB
testcase_11 AC 615 ms
58,916 KB
testcase_12 AC 669 ms
58,916 KB
testcase_13 AC 616 ms
58,916 KB
testcase_14 AC 667 ms
58,916 KB
testcase_15 AC 577 ms
39,972 KB
testcase_16 AC 616 ms
39,972 KB
testcase_17 AC 570 ms
39,972 KB
testcase_18 AC 350 ms
49,572 KB
testcase_19 AC 451 ms
58,660 KB
testcase_20 AC 403 ms
58,660 KB
testcase_21 AC 514 ms
206,608 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (110 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.0/publish/

ソースコード

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);
        c = NMi;
        var w = NList;
        var q = NN;
        var query = NMap(q);
        var INF = long.MaxValue / 2;
        var ans = Enumerable.Repeat(INF, q).ToArray();
        var bitmax = 1024;
        for (var b = 1; b < bitmax; ++b)
        {
            var tmp = b;
            var list = new bool[10];
            var cost = 0L;
            for (var i = 0; i < 10; ++i)
            {
                if (tmp % 2 == 1)
                {
                    list[i] = true;
                    cost += w[i];
                }
                tmp >>= 1;
            }
            var uf = new UnionFindTree(n);
            foreach (var edge in map)
            {
                if (list[c[edge[0]]] && list[c[edge[1]]]) uf.Unite(edge[0], edge[1]);
            }
            for (var i = 0; i < q; ++i)
            {
                if (uf.IsSameTree(query[i][0], query[i][1])) ans[i] = Math.Min(ans[i], cost);
            }
        }
        WriteLine(string.Join("\n", ans.Select(ai => ai == INF ? -1 : ai)));
    }
    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