結果

問題 No.1647 Travel in Mitaru city 2
ユーザー 👑 kakel-sankakel-san
提出日時 2024-01-23 23:35:23
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 523 ms / 2,500 ms
コード長 10,026 bytes
コンパイル時間 7,674 ms
コンパイル使用メモリ 158,424 KB
実行使用メモリ 225,740 KB
最終ジャッジ日時 2024-01-23 23:35:57
合計ジャッジ時間 32,096 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
33,700 KB
testcase_01 AC 89 ms
33,316 KB
testcase_02 AC 100 ms
33,700 KB
testcase_03 AC 101 ms
33,956 KB
testcase_04 AC 109 ms
35,748 KB
testcase_05 AC 101 ms
33,828 KB
testcase_06 AC 101 ms
33,956 KB
testcase_07 AC 127 ms
37,796 KB
testcase_08 AC 509 ms
100,916 KB
testcase_09 AC 457 ms
98,700 KB
testcase_10 AC 506 ms
97,828 KB
testcase_11 AC 465 ms
98,252 KB
testcase_12 AC 446 ms
92,888 KB
testcase_13 AC 470 ms
97,832 KB
testcase_14 AC 497 ms
94,852 KB
testcase_15 AC 517 ms
94,296 KB
testcase_16 AC 467 ms
98,764 KB
testcase_17 AC 490 ms
97,168 KB
testcase_18 AC 452 ms
92,328 KB
testcase_19 AC 509 ms
91,940 KB
testcase_20 AC 465 ms
89,384 KB
testcase_21 AC 468 ms
88,868 KB
testcase_22 AC 514 ms
96,364 KB
testcase_23 AC 488 ms
96,492 KB
testcase_24 AC 495 ms
94,376 KB
testcase_25 AC 465 ms
94,120 KB
testcase_26 AC 519 ms
96,612 KB
testcase_27 AC 493 ms
97,128 KB
testcase_28 AC 523 ms
98,192 KB
testcase_29 AC 503 ms
98,192 KB
testcase_30 AC 474 ms
96,172 KB
testcase_31 AC 519 ms
98,196 KB
testcase_32 AC 466 ms
96,168 KB
testcase_33 AC 503 ms
93,864 KB
testcase_34 AC 482 ms
98,192 KB
testcase_35 AC 485 ms
92,200 KB
testcase_36 AC 483 ms
98,192 KB
testcase_37 AC 514 ms
98,192 KB
testcase_38 AC 463 ms
89,640 KB
testcase_39 AC 448 ms
88,744 KB
testcase_40 AC 484 ms
89,256 KB
testcase_41 AC 438 ms
86,568 KB
testcase_42 AC 498 ms
89,512 KB
testcase_43 AC 87 ms
33,188 KB
testcase_44 AC 100 ms
33,700 KB
testcase_45 AC 396 ms
76,588 KB
testcase_46 AC 406 ms
76,456 KB
testcase_47 AC 390 ms
76,584 KB
testcase_48 AC 394 ms
76,588 KB
testcase_49 AC 422 ms
76,588 KB
testcase_50 AC 391 ms
225,740 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (102 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();
        // Test();
    }
    static void Test()
    {
        var r = new Random();
        var count = 0;
        while (true)
        {
            var h = 10000;
            var w = 10000;
            var n = 10000;
            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 inlist = new HashSet<int>(si);
                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);
                    for (var i = 0; i < ntree[cur].Count; ++i)
                    {
                        if (inlist.Contains(ntree[cur][i]))
                        {
                            cur = ntree[cur][i];
                            break;
                        }
                    }
                }
            }
        }
        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