結果

問題 No.2780 The Bottle Imp
ユーザー kakel-sankakel-san
提出日時 2024-06-07 22:22:21
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 241 ms / 2,000 ms
コード長 5,106 bytes
コンパイル時間 12,382 ms
コンパイル使用メモリ 169,540 KB
実行使用メモリ 191,712 KB
最終ジャッジ日時 2024-06-08 10:33:22
合計ジャッジ時間 19,680 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
30,332 KB
testcase_01 AC 64 ms
30,720 KB
testcase_02 AC 64 ms
30,720 KB
testcase_03 AC 65 ms
30,720 KB
testcase_04 AC 63 ms
30,208 KB
testcase_05 AC 62 ms
30,844 KB
testcase_06 AC 63 ms
30,848 KB
testcase_07 AC 148 ms
50,048 KB
testcase_08 AC 148 ms
50,016 KB
testcase_09 AC 151 ms
50,304 KB
testcase_10 AC 144 ms
49,920 KB
testcase_11 AC 144 ms
49,788 KB
testcase_12 AC 208 ms
62,876 KB
testcase_13 AC 210 ms
62,972 KB
testcase_14 AC 105 ms
43,136 KB
testcase_15 AC 111 ms
43,008 KB
testcase_16 AC 104 ms
42,880 KB
testcase_17 AC 105 ms
43,008 KB
testcase_18 AC 106 ms
43,136 KB
testcase_19 AC 105 ms
42,876 KB
testcase_20 AC 106 ms
43,008 KB
testcase_21 AC 105 ms
42,876 KB
testcase_22 AC 97 ms
39,936 KB
testcase_23 AC 102 ms
42,200 KB
testcase_24 AC 140 ms
52,864 KB
testcase_25 AC 178 ms
55,808 KB
testcase_26 AC 128 ms
48,128 KB
testcase_27 AC 128 ms
48,128 KB
testcase_28 AC 125 ms
47,832 KB
testcase_29 AC 119 ms
51,200 KB
testcase_30 AC 128 ms
52,320 KB
testcase_31 AC 179 ms
58,592 KB
testcase_32 AC 89 ms
38,400 KB
testcase_33 AC 211 ms
82,304 KB
testcase_34 AC 241 ms
84,096 KB
testcase_35 AC 86 ms
36,596 KB
testcase_36 AC 66 ms
30,208 KB
testcase_37 AC 64 ms
30,424 KB
testcase_38 AC 83 ms
36,608 KB
testcase_39 AC 161 ms
62,684 KB
testcase_40 AC 164 ms
62,516 KB
testcase_41 AC 164 ms
62,848 KB
testcase_42 AC 63 ms
30,964 KB
testcase_43 AC 62 ms
191,712 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (117 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[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var n = NN;
        var map = NArr(n);
        WriteLine(Bottle(n, map) ? "Yes" : "No");
    }
    static bool Bottle(int n, int[][] map)
    {
        var g = new SCCGraph(n);
        for (var i = 0; i < n; ++i) for (var j = 1; j < map[i].Length; ++j)
        {
            g.AddEdge(i, map[i][j] - 1);
        }
        var scc = g.SCC();
        var gid = new int[n];
        for (var i = 0; i < scc.Length; ++i) foreach (var s in scc[i]) gid[s] = i;
        var tree = new List<int>[scc.Length];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<int>();
        var refs = new int[scc.Length];
        for (var i = 0; i < n; ++i) for (var j = 1; j < map[i].Length; ++j)
        {
            if (gid[i] != gid[map[i][j] - 1])
            {
                tree[gid[i]].Add(gid[map[i][j] - 1]);
                ++refs[gid[map[i][j] - 1]];
            }
        }
        if (refs[gid[0]] > 0)
        {
            return false;
        }
        var list = new List<int>();
        var q = new Queue<int>();
        for (var i = 0; i < refs.Length; ++i) if (refs[i] == 0)
        {
            list.Add(i);
            q.Enqueue(i);
        }
        if (q.Count > 1)
        {
            return false;
        }
        while (q.Count > 0)
        {
            var cur = q.Dequeue();
            foreach (var next in tree[cur])
            {
                --refs[next];
                if (refs[next] == 0)
                {
                    list.Add(next);
                    q.Enqueue(next);
                    if (q.Count > 1)
                    {
                        return false;
                    }
                }
            }
        }
        return list.Count == scc.Length;
    }
    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;
            }
        }
    }
}
0