結果
問題 | No.1507 Road Blocked |
ユーザー |
![]() |
提出日時 | 2021-05-16 05:41:08 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 98 ms / 2,000 ms |
コード長 | 1,113 bytes |
コンパイル時間 | 825 ms |
コンパイル使用メモリ | 96,616 KB |
実行使用メモリ | 14,888 KB |
最終ジャッジ日時 | 2024-10-04 05:38:27 |
合計ジャッジ時間 | 5,151 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#include <iostream> #include <string> #include <algorithm> #include <vector> #include <iomanip> #include <cmath> #include <stdio.h> #include <queue> #include <deque> #include <cstdio> #include <set> #include <map> #include <bitset> #include <stack> #include <cctype> using namespace std; long long n; vector<vector<int>> vec(100010); bool bo[100010] = { false }, bo1[100010] = { true }; long long mod = 998244353, ans = 0; long long dfs(int now, int mem) { long long sum = 0; for (int i = 0; i < vec[now].size(); i++) { if (vec[now][i] == mem)continue; sum += dfs(vec[now][i], now); } ans += (sum + 1) * (n - sum - 1); return sum + 1; } long long m_pow(long long x, long long y) { long long ans = 1; while (y > 0) { if ((y & 1) == 1) { ans = ans * x % mod; } x = x * x % mod; y >>= 1; } return ans; } int main() { cin >> n; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; vec[a].emplace_back(b); vec[b].emplace_back(a); } dfs(1, -1); ans = n * (n - 1) / 2 * (n - 1) - ans; ans %= mod; cout << ans * m_pow(n * (n - 1) / 2 * (n - 1) % mod, mod - 2) % mod << endl; }