結果

問題 No.1647 Travel in Mitaru city 2
ユーザー 👑 kakel-sankakel-san
提出日時 2024-01-23 21:53:27
言語 C#
(.NET 8.0.203)
結果
WA  
実行時間 -
コード長 3,617 bytes
コンパイル時間 6,943 ms
コンパイル使用メモリ 158,804 KB
実行使用メモリ 209,372 KB
最終ジャッジ日時 2024-01-23 21:54:06
合計ジャッジ時間 28,149 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
33,316 KB
testcase_01 AC 81 ms
33,060 KB
testcase_02 AC 85 ms
33,316 KB
testcase_03 WA -
testcase_04 AC 88 ms
34,084 KB
testcase_05 AC 84 ms
33,316 KB
testcase_06 WA -
testcase_07 AC 92 ms
34,852 KB
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 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 290 ms
64,036 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 79 ms
33,060 KB
testcase_44 AC 86 ms
33,316 KB
testcase_45 WA -
testcase_46 AC 270 ms
61,220 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (97 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();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (h, w, n) = (c[0], c[1], c[2]);
        var map = new List<(int id, int h, int w)>(n);
        for (var i = 0; i < n; ++i)
        {
            c = NList;
            map.Add((i, c[0] - 1, c[1] - 1));
        }
        map.Sort((l, r) =>
        {
            var d = l.h.CompareTo(r.h);
            if (d != 0) return d;
            return l.w.CompareTo(r.w);
        });
        var tree = new List<int>[n];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<int>();
        for (var i = 0; i + 1 < map.Count; ++i)
        {
            if (map[i].h == map[i + 1].h)
            {
                tree[map[i].id].Add(map[i + 1].id);
                tree[map[i + 1].id].Add(map[i].id);
            }
        }
        map.Sort((l, r) =>
        {
            var d = l.w.CompareTo(r.w);
            if (d != 0) return d;
            return l.h.CompareTo(r.h);
        });
        for (var i = 0; i + 1 < map.Count; ++i)
        {
            if (map[i].w == map[i + 1].w)
            {
                tree[map[i].id].Add(map[i + 1].id);
                tree[map[i + 1].id].Add(map[i].id);
            }
        }
        map.Sort((l, r) => l.id.CompareTo(r.id));
        var plist = Enumerable.Repeat(-1, n).ToArray();
        for (var i = 0; i < n; ++i) if (plist[i] < 0)
        {
            var q = new Queue<(int pos, int prev)>();
            foreach (var next in tree[i])
            {
                plist[next] = i;
                q.Enqueue((next, i));
            }
            while (q.Count > 0)
            {
                var cur = q.Dequeue();
                foreach (var next in tree[cur.pos])
                {
                    if (next == cur.prev) continue;
                    if (plist[next] >= 0)
                    {
                        // ループ検知
                        var ans = Ans(i, cur.pos, next, plist, map);
                        WriteLine(ans.Count);
                        WriteLine(string.Join(" ", ans.Select(ai => ai + 1)));
                        return;
                    }
                    plist[next] = cur.pos;
                    q.Enqueue((next, cur.pos));
                }
            }
        }
        WriteLine(-1);
    }
    static List<int> Ans(int start, int cur, int next, int[] plist, List<(int id, int h, int w)> map)
    {
        var pos = cur;
        var li1 = new List<int>();
        while (pos != start)
        {
            li1.Add(pos);
            pos = plist[pos];
        }
        pos = next;
        var li2 = new List<int>();
        while (pos >= 0)
        {
            li2.Add(pos);
            pos = plist[pos];
        }
        li2.Reverse();
        li1.AddRange(li2);
        var ans = new List<int>();
        for (var i = 0; i < li1.Count; ++i)
        {
            var pi = (li1.Count - 1 + i) % li1.Count;
            var ni = (i + 1) % li1.Count;
            if ((map[li1[pi]].h == map[li1[i]].h) != (map[li1[i]].h == map[li1[ni]].h)) ans.Add(li1[pi]);
        }
        if (map[ans[0]].h == map[ans[1]].h)
        {
            ans.Add(ans[0]);
            ans.RemoveAt(0);
        }
        return ans;
    }
}
0