結果

問題 No.1507 Road Blocked
ユーザー 👑 kakel-sankakel-san
提出日時 2024-06-16 22:58:16
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 244 ms / 2,000 ms
コード長 1,722 bytes
コンパイル時間 7,391 ms
コンパイル使用メモリ 166,408 KB
実行使用メモリ 214,744 KB
最終ジャッジ日時 2024-06-16 22:58:39
合計ジャッジ時間 15,335 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
30,172 KB
testcase_01 AC 58 ms
30,456 KB
testcase_02 AC 56 ms
30,204 KB
testcase_03 AC 186 ms
74,148 KB
testcase_04 AC 189 ms
60,112 KB
testcase_05 AC 197 ms
60,096 KB
testcase_06 AC 222 ms
59,980 KB
testcase_07 AC 192 ms
60,112 KB
testcase_08 AC 185 ms
60,092 KB
testcase_09 AC 193 ms
59,984 KB
testcase_10 AC 190 ms
60,228 KB
testcase_11 AC 191 ms
60,104 KB
testcase_12 AC 209 ms
59,996 KB
testcase_13 AC 177 ms
59,976 KB
testcase_14 AC 185 ms
59,980 KB
testcase_15 AC 188 ms
60,112 KB
testcase_16 AC 187 ms
60,088 KB
testcase_17 AC 184 ms
60,096 KB
testcase_18 AC 211 ms
60,116 KB
testcase_19 AC 186 ms
60,100 KB
testcase_20 AC 194 ms
59,972 KB
testcase_21 AC 187 ms
59,976 KB
testcase_22 AC 190 ms
60,112 KB
testcase_23 AC 186 ms
60,112 KB
testcase_24 AC 228 ms
59,852 KB
testcase_25 AC 195 ms
59,980 KB
testcase_26 AC 190 ms
59,988 KB
testcase_27 AC 244 ms
59,984 KB
testcase_28 AC 202 ms
59,980 KB
testcase_29 AC 241 ms
60,124 KB
testcase_30 AC 194 ms
60,100 KB
testcase_31 AC 193 ms
59,984 KB
testcase_32 AC 201 ms
214,744 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (94 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[] 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 n = NN;
        var map = NMap(n - 1);
        var tree = new List<int>[n];
        for (var i = 0; i < n; ++i) tree[i] = new List<int>();
        foreach (var edge in map)
        {
            tree[edge[0]].Add(edge[1]);
            tree[edge[1]].Add(edge[0]);
        }
        var ans = 0L;
        DFS(n, 0, -1, tree, ref ans);
        var mod = 998_244_353;
        ans %= mod;
        var all = (long)n * (n - 1) / 2 % mod * (n - 1) % mod;
        WriteLine((all - ans + mod) % mod * Exp(all, mod - 2, mod) % mod);
    }
    static int DFS(int n, int cur, int prev, List<int>[] tree, ref long ans)
    {
        var count = 1;
        foreach (var next in tree[cur])
        {
            if (prev == next) continue;
            var num = DFS(n, next, cur, tree, ref ans);
            ans += (long)num * (n - num);
            count += num;
        }
        return count;
    }
    static long Exp(long n, long p, int mod)
    {
        long _n = n % mod;
        var _p = p;
        var result = 1L;
        if ((_p & 1) == 1) result *= _n;
        while (_p > 0)
        {
            _n = _n * _n % mod;
            _p >>= 1;
            if ((_p & 1) == 1) result = result * _n % mod;
        }
        return result;
    }
}
0