結果

問題 No.1647 Travel in Mitaru city 2
ユーザー kakel-sankakel-san
提出日時 2024-01-23 23:25:09
言語 C#
(.NET 8.0.203)
結果
RE  
実行時間 -
コード長 9,697 bytes
コンパイル時間 8,659 ms
コンパイル使用メモリ 167,812 KB
実行使用メモリ 833,668 KB
最終ジャッジ日時 2024-09-28 06:54:36
合計ジャッジ時間 84,410 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
32,128 KB
testcase_01 AC 65 ms
31,488 KB
testcase_02 AC 82 ms
32,128 KB
testcase_03 AC 73 ms
32,384 KB
testcase_04 AC 85 ms
33,920 KB
testcase_05 AC 75 ms
32,000 KB
testcase_06 AC 72 ms
32,512 KB
testcase_07 AC 98 ms
35,584 KB
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 J_TLE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 AC 65 ms
31,104 KB
testcase_44 AC 73 ms
32,256 KB
testcase_45 RE -
testcase_46 AC 496 ms
74,252 KB
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 MLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (104 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.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();
        // Test();
    }
    static void Test()
    {
        var r = new Random();
        var count = 0;
        while (true)
        {
            var h = 100;
            var w = 100;
            var n = 8;
            var map = Make(r, h, w, n);
            // 愚直解
            // var s1 = All(n, t, map);
            // 作成解
            var s2 = Tour(h, w, n, map);
            if (s2 != "-1" && !Check(s2, map))
            {
                WriteLine($"{h} {w} {n}");
                WriteLine(string.Join("\n", map.Select(m => string.Join(" ", m))));
                // WriteLine(s1);
                WriteLine(s2);
                WriteLine("is not good travel");
                return;
            }
            ++count;
            if (count % 1000 == 0) WriteLine(count);
        }
    }
    static int[][] Make(Random r, int h, int w, int n)
    {
        var set = new HashSet<int>();
        var ans = new int[n][];
        for (var i = 0; i < n; ++i)
        {
            while (true)
            {
                var x = r.Next(h);
                var y = r.Next(w);
                if (set.Add(x * w + y))
                {
                    ans[i] = new int[] { x, y };
                    break;
                }
            }
        }
        return ans;
    }
    static bool Check(string ans, int[][] map)
    {
        if (ans == "-1") return true;
        var c = ans.Split("\n");
        var t = c[1].Split();
        if (t.Length < 4) return false;
        var set = new HashSet<int>();
        for (var i = 0; i < t.Length; ++i)
        {
            var a = int.Parse(t[i]) - 1;
            var b = int.Parse(t[(i + 1) % t.Length]) - 1;
            if (!set.Add(a)) return false;
            if (i % 2 == 0)
            {
                if (map[a][1] != map[b][1]) return false;
            }
            else
            {
                if (map[a][0] != map[b][0]) return false;
            }
        }
        return true;
    }
    static void Solve()
    {
        var c = NList;
        var (h, w, n) = (c[0], c[1], c[2]);
        var map = NMap(n);
        var ans = Tour(h, w, n, map);
        // if(Check(ans, map))
        // {
             WriteLine(ans);
        //     WriteLine("OK");
        // }
        // else
        // {
        //     WriteLine("!!!");
        // }
    }
    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 visited = new bool[n];
        var ntree = new List<int>[n];
        for (var i = 0; i < ntree.Length; ++i) ntree[i] = new List<int>();
        for (var i = 0; i < n; ++i) if (!visited[i])
        {
            var set = new HashSet<long>();
            DFS(i, tree, set, ntree, visited);
        }
        // WriteLine(string.Join("\n", ntree.Select((ni, id) => $"{id}: {string.Join(" ", ni)}")));
        var scc = new SCCGraph(n);
        for (var i = 0; i < n; ++i) foreach (var next in ntree[i]) scc.AddEdge(i, next);
        var slist = scc.SCC();
        foreach (var si in slist)
        {
            // WriteLine($"scc {string.Join(" ", si)}");
            if (si.Count >= 4)
            {
                var set = new HashSet<int>();
                var list = new List<int>();
                var cur = si[0];
                while (true)
                {
                    if (!set.Add(cur))
                    {
                        var pos = list.IndexOf(cur);
                        var ans = Ans(list.Skip(pos).ToList(), map);
                        return $"{ans.Count}\n{string.Join(" ", ans.Select(ai => ai + 1))}";
                    }
                    list.Add(cur);
                    cur = ntree[cur][0];
                }
            }
        }
        return "-1";
    }
    static void DFS(int cur, List<int>[] tree, HashSet<long> set, List<int>[] ntree, bool[] visited)
    {
        visited[cur] = true;
        foreach (var next in tree[cur])
        {
            if (set.Add(Math.Min(cur, next) * 1000000L + Math.Max(cur, next)))
            {
                ntree[cur].Add(next);
                DFS(next, tree, set, ntree, visited);
            }
        }
    }
    class SCCGraph
    {
        private int _n;
        private List<(int from, int to)> edges;
        public SCCGraph(int n)
        {
            _n = n;
            edges = new List<(int, int)>();
        }
        public int NumVertices() { return _n; }
        public void AddEdge(int from, int to) { edges.Add((from, to )); }
        public (int, int[]) SCCIds()
        {
            var g = new CSR<int>(_n, edges);
            var nowOrd = 0;
            var groupNum = 0;
            var visited = new List<int>(_n);
            var low = new int[_n];
            var ord = Enumerable.Repeat(-1, _n).ToArray();
            var ids = new int[_n];
            void DFS(int v)
            {
                low[v] = ord[v] = nowOrd++;
                visited.Add(v);
                for (var i = g.Start[v]; i < g.Start[v + 1]; ++i)
                {
                    var to = g.EList[i];
                    if (ord[to] == -1)
                    {
                        DFS(to);
                        low[v] = Math.Min(low[v], low[to]);
                    }
                    else low[v] = Math.Min(low[v], ord[to]);
                }
                if (low[v] == ord[v])
                {
                    while (true)
                    {
                        var u = visited.Last();
                        visited.RemoveAt(visited.Count - 1);
                        ord[u] = _n;
                        ids[u] = groupNum;
                        if (u == v) break;
                    }
                    ++groupNum;
                }
            }
            for (var i = 0; i < _n; ++i) if (ord[i] == -1) DFS(i);
            for (var i = 0; i < ids.Length; ++i) ids[i] = groupNum - 1 - ids[i];
            return (groupNum, ids);
        }
        public List<int>[] SCC()
        {
            var (groupNum, list) = SCCIds();
            var counts = new int[groupNum];
            foreach (var x in list) ++counts[x];
            var groups = new List<int>[groupNum];
            for (var i = 0; i < groups.Length; ++i) groups[i] = new List<int>(counts[i]);
            for (var i = 0; i < _n; ++i) groups[list[i]].Add(i);
            return groups;
        }
    }
    // 有向辺のリスト
    class CSR<E>
    {
        public int[] Start { get; private set; }
        public E[] EList { get; private set; }
        public CSR(int n, List<(int, E)> edges)
        {
            Start = new int[n + 1];
            EList = new E[edges.Count];
            foreach (var e in edges) ++Start[e.Item1 + 1];
            for (var i = 1; i <= n; ++i) Start[i] += Start[i - 1];
            var counter = (int[])Start.Clone();
            foreach (var e in edges)
            {
                EList[counter[e.Item1]++] = e.Item2;
            }
        }
    }
    static List<int> Ans(List<int> list, List<(int id, int h, int w)> map)
    {
        var ans = new List<int>();
        var begin = -1;
        for (var i = 0; i < list.Count; ++i)
        {
            var pi = (list.Count - 1 + i) % list.Count;
            var ni = (i + 1) % list.Count;
            if (map[list[pi]].h == map[list[i]].h && map[list[i]].w == map[list[ni]].w)
            {
                ans.Add(list[i]);
                begin = i;
                break;
            }
        }
        var tmp = (begin + 1) % list.Count;
        while (tmp != begin)
        {
            var pi = (list.Count - 1 + tmp) % list.Count;
            var ni = (tmp + 1) % list.Count;
            if ((map[list[pi]].h == map[list[tmp]].h) != (map[list[tmp]].h == map[list[ni]].h)) ans.Add(list[tmp]);
            tmp = (tmp + 1) % list.Count;
        }
        return ans;
    }
}
0