結果

問題 No.2780 The Bottle Imp
ユーザー kakel-sankakel-san
提出日時 2024-06-07 22:09:23
言語 C#
(.NET 8.0.203)
結果
WA  
実行時間 -
コード長 4,996 bytes
コンパイル時間 11,981 ms
コンパイル使用メモリ 166,884 KB
実行使用メモリ 192,664 KB
最終ジャッジ日時 2024-06-08 10:30:52
合計ジャッジ時間 18,896 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
30,848 KB
testcase_01 AC 57 ms
30,848 KB
testcase_02 AC 58 ms
30,696 KB
testcase_03 AC 58 ms
30,720 KB
testcase_04 AC 57 ms
30,592 KB
testcase_05 AC 59 ms
30,592 KB
testcase_06 AC 61 ms
30,464 KB
testcase_07 AC 150 ms
50,688 KB
testcase_08 AC 145 ms
50,560 KB
testcase_09 AC 144 ms
50,688 KB
testcase_10 AC 143 ms
50,672 KB
testcase_11 AC 141 ms
50,668 KB
testcase_12 AC 218 ms
63,104 KB
testcase_13 AC 211 ms
63,360 KB
testcase_14 AC 106 ms
43,520 KB
testcase_15 AC 105 ms
43,520 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 108 ms
43,392 KB
testcase_19 AC 108 ms
43,520 KB
testcase_20 AC 107 ms
43,520 KB
testcase_21 WA -
testcase_22 AC 101 ms
40,448 KB
testcase_23 AC 102 ms
42,880 KB
testcase_24 AC 144 ms
53,248 KB
testcase_25 AC 171 ms
56,832 KB
testcase_26 AC 128 ms
48,492 KB
testcase_27 AC 125 ms
48,384 KB
testcase_28 AC 127 ms
48,640 KB
testcase_29 AC 126 ms
51,960 KB
testcase_30 AC 137 ms
53,120 KB
testcase_31 AC 172 ms
59,136 KB
testcase_32 AC 92 ms
39,032 KB
testcase_33 AC 198 ms
82,816 KB
testcase_34 AC 217 ms
84,608 KB
testcase_35 AC 87 ms
37,120 KB
testcase_36 AC 57 ms
30,720 KB
testcase_37 AC 61 ms
30,828 KB
testcase_38 AC 88 ms
37,120 KB
testcase_39 AC 153 ms
63,224 KB
testcase_40 AC 155 ms
63,104 KB
testcase_41 AC 153 ms
63,096 KB
testcase_42 AC 59 ms
30,828 KB
testcase_43 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (97 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);
        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]];
            }
        }
        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)
        {
            WriteLine("No");
            return;
        }
        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)
                    {
                        WriteLine("No");
                        return;
                    }
                }
            }
        }
        WriteLine(list.Count == scc.Length ? "Yes" : "No");
    }
    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