結果

問題 No.1473 おでぶなおばけさん
ユーザー bluemeganebluemegane
提出日時 2021-04-11 11:26:42
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 3,111 bytes
コンパイル時間 1,360 ms
コンパイル使用メモリ 66,180 KB
実行使用メモリ 52,472 KB
最終ジャッジ日時 2023-09-09 14:13:48
合計ジャッジ時間 17,353 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
23,888 KB
testcase_01 AC 68 ms
21,832 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 297 ms
45,992 KB
testcase_23 AC 267 ms
45,356 KB
testcase_24 AC 244 ms
43,088 KB
testcase_25 AC 313 ms
47,436 KB
testcase_26 AC 292 ms
45,916 KB
testcase_27 AC 156 ms
35,624 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 232 ms
42,484 KB
testcase_37 AC 239 ms
40,916 KB
testcase_38 AC 98 ms
30,076 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

using System.Collections.Generic;
using System.Linq;
using System;

public class UnionFind
{
    private int[] data;
    public UnionFind(int size)
    {
        data = new int[size];
        for (int i = 0; i < size; i++) data[i] = -1;
    }
    public bool Unite(int x, int y)
    {
        x = Root(x);
        y = Root(y);
        if (x != y)
        {
            if (data[y] < data[x])
            {
                var tmp = y;
                y = x;
                x = tmp;
            }
            data[x] += data[y];
            data[y] = x;
        }
        return x != y;
    }
    public bool IsSameGroup(int x, int y) => Root(x) == Root(y);
    public int Root(int x) => data[x] < 0 ? x : data[x] = Root(data[x]);
    public int getMem(int x) => -data[Root(x)];
}

public class E2
{
    public int n1 { get; set; }
    public int n2 { get; set; }
    public int d { get; set; }
}


public class Edge
{
    public int to { get; set; }
    public int d { get; set; }
}

public class P
{
    public int from { get; set; }
    public int step { get; set; }
}

public class Hello
{
    public static int n, m;
    static void Main()
    {
        string[] line = Console.ReadLine().Trim().Split(' ');
        n = int.Parse(line[0]);
        m = int.Parse(line[1]);
        var aa = new List<Edge>[n];
        for (int i = 0; i < n; i++) aa[i] = new List<Edge>();
        var e2s = new E2[m];
        for (int i = 0; i < m; i++)
        {
            line = Console.ReadLine().Trim().Split(' ');
            var s = int.Parse(line[0]) - 1;
            var t = int.Parse(line[1]) - 1;
            var d = int.Parse(line[2]);
            aa[s].Add(new Edge { to = t, d = d });
            aa[t].Add(new Edge { to = s, d = d });
            e2s[i] = new E2 { n1 = s, n2 = t, d = d };
        }
        var e0 = new List<int>();
        foreach (var x in aa[0]) e0.Add(x.to);
        var W = getW(e2s, e0);
        var step = getSTEP(aa, W);
        Console.WriteLine("{0} {1}", W, step);
    }
    static int getW(E2[] e2s, List<int> e0)
    {
        var uf = new UnionFind(n);
        e2s = e2s.OrderByDescending(x => x.d).ToArray();
        uf.Unite(e2s[0].n1, e2s[0].n2);
        var p = uf.Root(e2s[0].n1);
        for (int i = 1; i < m; i++)
        {
            uf.Unite(p, e2s[i].n1);
            uf.Unite(p, e2s[i].n2);
            foreach (var x in e0)
            {
                if (uf.Root(x) == p) return e2s[i].d;
            }
        }
        return 0;
    }

    static int getSTEP(List<Edge>[] aa, int t)
    {
        var visited = new bool[n];
        var q = new Queue<P>();
        q.Enqueue(new P { from = 0, step = 0 });
        visited[0] = true;
        while (q.Count() > 0)
        {
            var w = q.Dequeue();
            if (w.from == n - 1) return w.step;
            foreach (var x in aa[w.from])
            {
                if (t <= x.d && !visited[x.to])
                {
                    q.Enqueue(new P { from = x.to, step = w.step + 1 });
                    visited[x.to] = true;
                }
            }
        }
        return 0;
    }
}
0