結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー tobisatistobisatis
提出日時 2024-02-16 22:36:34
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 2,409 ms / 2,500 ms
コード長 3,840 bytes
コンパイル時間 8,048 ms
コンパイル使用メモリ 158,212 KB
実行使用メモリ 270,012 KB
最終ジャッジ日時 2024-02-16 22:37:24
合計ジャッジ時間 44,489 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 728 ms
53,796 KB
testcase_01 AC 1,509 ms
77,604 KB
testcase_02 AC 2,376 ms
111,624 KB
testcase_03 AC 81 ms
33,956 KB
testcase_04 AC 80 ms
33,956 KB
testcase_05 AC 82 ms
33,956 KB
testcase_06 AC 1,436 ms
46,244 KB
testcase_07 AC 2,326 ms
118,104 KB
testcase_08 AC 2,253 ms
109,064 KB
testcase_09 AC 2,316 ms
119,224 KB
testcase_10 AC 2,368 ms
119,232 KB
testcase_11 AC 2,380 ms
119,224 KB
testcase_12 AC 2,409 ms
119,232 KB
testcase_13 AC 2,354 ms
119,240 KB
testcase_14 AC 2,340 ms
119,240 KB
testcase_15 AC 752 ms
38,564 KB
testcase_16 AC 755 ms
38,564 KB
testcase_17 AC 725 ms
38,564 KB
testcase_18 AC 1,544 ms
59,940 KB
testcase_19 AC 1,909 ms
99,560 KB
testcase_20 AC 1,830 ms
88,128 KB
testcase_21 AC 2,013 ms
270,012 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (90 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 #

namespace AtCoder;

#nullable enable

using System.Numerics;

class UnionFind
{
    (int parent, int size)[] Verticals { get; set; }

    public UnionFind(int verticals)
    {
        Verticals = new (int, int)[verticals];
        for (int i = 0; i < verticals; i++) Verticals[i] = (i, 1);
    }

    public int Union(int x, int y)
    {
        (x, y) = (Find(x), Find(y));
        if (Size(x) < Size(y)) (x, y) = (y, x);
        if (x == y) return x;
        Verticals[x].size += Verticals[y].size;
        Verticals[y].parent = x;
        return x;
    }

    public int Find(int x) => Parent(x).parent;
    public int Size(int x) => Parent(x).size;

    (int parent, int size) Parent(int x)
    {
        var parentId = Verticals[x].parent;
        if (parentId == x) return Verticals[x];
        var parent = Parent(parentId);
        Verticals[x].parent = parent.parent;
        return parent;
    }
}

static class Extensions
{
    public static T[] Repeat<T>(this int time, Func<T> F) => Enumerable.Range(0, time).Select(_ => F()).ToArray();

    public static int ToFlag(this int i)
    {
        if (i < 0 || 32 <= i) throw new IndexOutOfRangeException();
        return 1 << i;
    }
    public static bool OfFlag(this int value, int i) => (value & ToFlag(i)) > 0;
    public static int WithFlag(this int value, int i, bool f) => f ? (value | ToFlag(i)) : (value & ~ToFlag(i));
}

class AtCoder
{
    object? Solve()
    {
        var n = Int();
        var m = Int();
        var edges = m.Repeat(() => (Int() - 1, Int() - 1));
        var cz = n.Repeat(() => Int() - 1);
        var wz = 10.Repeat(Int);
        var q = Int();
        var ans = new long[q];
        var b = 1 << 10;
        var ufz = b.Repeat(() => new UnionFind(n));
        var csumz = new long[b];
        for (var i = 0; i < b; i++)
        {
            var csum = 0L;
            for (var j = 0; j < 10; j++) if (i.OfFlag(j)) csum += wz[j];
            csumz[i] = csum;
        }
        for (var i = 0; i < b; i++)
        {
            foreach (var (u, v) in edges)
            {
                if (!i.OfFlag(cz[u]) || !i.OfFlag(cz[v])) continue;
                ufz[i].Union(u, v);
            }
        }
        for (var t = 0; t < q; t++)
        {
            var u = Int() - 1;
            var v = Int() - 1;
            var minc = long.MaxValue;
            for (var i = 0; i < b; i++)
            {
                if (ufz[i].Find(u) == ufz[i].Find(v)) minc = Math.Min(minc, csumz[i]);
            }
            if (minc == long.MaxValue) minc = -1;
            ans[t] = minc;
        }
        Out(ans);
        return null;
    }

    public static void Main() => new AtCoder().Run();
    public void Run()
    {
        var res = Solve();
        if (res != null)
        {
            if (res is bool yes) res = yes ? "Yes" : "No";
            sw.WriteLine(res);
        }
        sw.Flush();
    }

    string[] input = Array.Empty<string>();
    int iter = 0;
    readonly StreamWriter sw = new(Console.OpenStandardOutput()) { AutoFlush = false };

    #pragma warning disable IDE0051
    string String()
    {
        while (iter >= input.Length) (input, iter) = (Console.ReadLine()!.Split(' '), 0);
        return input[iter++];
    }
    T Input<T>() where T : IParsable<T> => T.Parse(String(), null);
    int Int() => Input<int>();
    void Out(object? x, string? separator = null)
    {
        separator ??= Environment.NewLine;
        if (x is System.Collections.IEnumerable obj and not string)
        {
            var firstLine = true;
            foreach (var item in obj)
            {
                if (!firstLine) sw.Write(separator);
                firstLine = false;
                sw.Write(item);
            }
        }
        else sw.Write(x);
        sw.WriteLine();
    }
    #pragma warning restore IDE0051
}
0