結果

問題 No.1647 Travel in Mitaru city 2
ユーザー 👑 kakel-sankakel-san
提出日時 2024-01-23 22:19:20
言語 C#
(.NET 8.0.203)
結果
WA  
実行時間 -
コード長 4,194 bytes
コンパイル時間 13,532 ms
コンパイル使用メモリ 158,160 KB
実行使用メモリ 212,516 KB
最終ジャッジ日時 2024-01-23 22:19:57
合計ジャッジ時間 35,380 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
33,316 KB
testcase_01 AC 86 ms
32,932 KB
testcase_02 AC 91 ms
33,316 KB
testcase_03 AC 92 ms
33,444 KB
testcase_04 AC 96 ms
34,212 KB
testcase_05 AC 92 ms
33,444 KB
testcase_06 AC 93 ms
33,444 KB
testcase_07 AC 101 ms
34,980 KB
testcase_08 AC 368 ms
74,160 KB
testcase_09 WA -
testcase_10 AC 366 ms
74,336 KB
testcase_11 WA -
testcase_12 AC 336 ms
67,460 KB
testcase_13 AC 332 ms
67,148 KB
testcase_14 AC 357 ms
71,540 KB
testcase_15 AC 379 ms
74,228 KB
testcase_16 AC 350 ms
73,632 KB
testcase_17 WA -
testcase_18 AC 389 ms
70,860 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 367 ms
72,524 KB
testcase_23 AC 382 ms
72,524 KB
testcase_24 AC 354 ms
69,072 KB
testcase_25 WA -
testcase_26 AC 384 ms
72,524 KB
testcase_27 AC 367 ms
72,656 KB
testcase_28 AC 366 ms
72,908 KB
testcase_29 AC 380 ms
72,908 KB
testcase_30 WA -
testcase_31 AC 376 ms
72,908 KB
testcase_32 AC 349 ms
69,580 KB
testcase_33 AC 359 ms
69,324 KB
testcase_34 AC 364 ms
72,908 KB
testcase_35 WA -
testcase_36 AC 367 ms
72,908 KB
testcase_37 AC 377 ms
72,908 KB
testcase_38 AC 355 ms
69,836 KB
testcase_39 AC 346 ms
67,532 KB
testcase_40 AC 371 ms
69,708 KB
testcase_41 AC 329 ms
66,380 KB
testcase_42 AC 361 ms
69,708 KB
testcase_43 AC 85 ms
32,932 KB
testcase_44 AC 91 ms
33,444 KB
testcase_45 WA -
testcase_46 AC 334 ms
65,484 KB
testcase_47 WA -
testcase_48 AC 338 ms
65,616 KB
testcase_49 WA -
testcase_50 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (100 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 (h, w, n) = (c[0], c[1], c[2]);
        var map = NMap(n);
        WriteLine(Tour(h, w, n, map));
    }
    static string Tour(int h, int w, int n, int[][] _map)
    {
        var map = new List<(int id, int h, int w)>(n);
        for (var i = 0; i < n; ++i)
        {
            map.Add((i, _map[i][0] - 1, _map[i][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);
                        return $"{ans.Count}\n{string.Join(" ", ans.Select(ai => ai + 1))}";
                    }
                    plist[next] = cur.pos;
                    q.Enqueue((next, cur.pos));
                }
            }
        }
        return "-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>();
        var begin = -1;
        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]].w == map[li1[ni]].w)
            {
                ans.Add(li1[i]);
                begin = i;
                break;
            }
        }
        var tmp = (begin + 1) % li1.Count;
        while (tmp != begin)
        {
            var pi = (li1.Count - 1 + tmp) % li1.Count;
            var ni = (tmp + 1) % li1.Count;
            if ((map[li1[pi]].h == map[li1[tmp]].h) != (map[li1[tmp]].h == map[li1[ni]].h)) ans.Add(li1[tmp]);
            tmp = (tmp + 1) % li1.Count;
        }
        return ans;
    }
}
0