結果

問題 No.1507 Road Blocked
ユーザー 👑 kakel-sankakel-san
提出日時 2024-06-16 22:54:10
言語 C#
(.NET 8.0.203)
結果
WA  
実行時間 -
コード長 1,701 bytes
コンパイル時間 8,552 ms
コンパイル使用メモリ 167,592 KB
実行使用メモリ 215,288 KB
最終ジャッジ日時 2024-06-16 22:54:29
合計ジャッジ時間 17,079 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
30,208 KB
testcase_01 AC 55 ms
30,208 KB
testcase_02 AC 55 ms
30,312 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (133 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;
        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