結果

問題 No.2531 Coloring Vertices on Namori
ユーザー kakel-sankakel-san
提出日時 2023-11-07 21:01:41
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 426 ms / 2,000 ms
コード長 5,049 bytes
コンパイル時間 11,213 ms
コンパイル使用メモリ 166,564 KB
実行使用メモリ 237,320 KB
最終ジャッジ日時 2024-09-25 23:27:29
合計ジャッジ時間 18,998 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
30,720 KB
testcase_01 AC 59 ms
30,848 KB
testcase_02 AC 59 ms
30,720 KB
testcase_03 AC 60 ms
30,592 KB
testcase_04 AC 59 ms
30,592 KB
testcase_05 AC 353 ms
118,236 KB
testcase_06 AC 59 ms
30,720 KB
testcase_07 AC 345 ms
117,980 KB
testcase_08 AC 391 ms
118,004 KB
testcase_09 AC 394 ms
117,896 KB
testcase_10 AC 391 ms
118,272 KB
testcase_11 AC 308 ms
76,532 KB
testcase_12 AC 314 ms
76,492 KB
testcase_13 AC 309 ms
76,660 KB
testcase_14 AC 417 ms
117,940 KB
testcase_15 AC 421 ms
117,860 KB
testcase_16 AC 426 ms
117,744 KB
testcase_17 AC 61 ms
30,848 KB
testcase_18 AC 60 ms
30,464 KB
testcase_19 AC 59 ms
30,720 KB
testcase_20 AC 405 ms
81,300 KB
testcase_21 AC 415 ms
81,300 KB
testcase_22 AC 392 ms
81,424 KB
testcase_23 AC 400 ms
81,424 KB
testcase_24 AC 397 ms
81,292 KB
testcase_25 AC 385 ms
81,300 KB
testcase_26 AC 401 ms
81,364 KB
testcase_27 AC 390 ms
81,296 KB
testcase_28 AC 387 ms
81,300 KB
testcase_29 AC 398 ms
81,508 KB
testcase_30 AC 389 ms
81,420 KB
testcase_31 AC 404 ms
81,428 KB
testcase_32 AC 393 ms
81,292 KB
testcase_33 AC 392 ms
237,320 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (89 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 List<int> NList => ReadLine().Split().Select(int.Parse).ToList();
    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 (n, k) = (c[0], c[1]);
        var map = NMap(n);
        WriteLine(Color(n, k, map));
    }
    static long Color(int n, int k, int[][] map)
    {
        var tree = new List<int>[n];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<int>();
        foreach (var edge in map)
        {
            tree[edge[0]].Add(edge[1]);
            tree[edge[1]].Add(edge[0]);
        }
        var g = new SCCGraph(n);
        var visited = new bool[n];
        CreateSCC(-1, 0, tree, visited, g);
        var scc = g.SCC();
        var loopsize = 0;
        for (var i = 0; i < scc.Length; ++i) if (scc[i].Count > 1)
        {
            loopsize = scc[i].Count;
            break;
        }
        var dp = new long[loopsize][];
        for (var i = 0; i < dp.Length; ++i) dp[i] = new long[2];
        dp[0][0] = 1;
        for (var i = 1; i < dp.Length; ++i)
        {
            if (i + 1 < dp.Length) dp[i][0] = dp[i - 1][1];
            dp[i][1] = (dp[i - 1][0] * (k - 1) % mod + dp[i - 1][1] * (k - 2) % mod) % mod;
        }
        var ans = (dp[^1][0] + dp[^1][1]) * k % mod;
        for (var i = loopsize; i < n; ++i) ans = ans * (k - 1) % mod;
        return ans;
    }
    static int mod = 998_244_353;
    static void CreateSCC(int prev, int cur, List<int>[] tree, bool[] visited, SCCGraph g)
    {
        if (visited[cur]) return;
        visited[cur] = true;
        foreach (var next in tree[cur])
        {
            if (prev == next) continue;
            g.AddEdge(cur, next);
            CreateSCC(cur, next, tree, visited, g);
        }
    }
    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