結果

問題 No.1507 Road Blocked
ユーザー startcppstartcpp
提出日時 2021-05-14 23:06:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 1,568 bytes
コンパイル時間 732 ms
コンパイル使用メモリ 73,468 KB
実行使用メモリ 18,428 KB
最終ジャッジ日時 2024-04-10 02:28:20
合計ジャッジ時間 4,847 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,744 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 76 ms
18,428 KB
testcase_04 AC 88 ms
12,612 KB
testcase_05 AC 87 ms
12,836 KB
testcase_06 AC 90 ms
12,740 KB
testcase_07 AC 91 ms
12,540 KB
testcase_08 AC 93 ms
12,612 KB
testcase_09 AC 92 ms
12,740 KB
testcase_10 AC 93 ms
12,744 KB
testcase_11 AC 89 ms
12,740 KB
testcase_12 AC 89 ms
12,572 KB
testcase_13 AC 94 ms
12,612 KB
testcase_14 AC 89 ms
12,612 KB
testcase_15 AC 90 ms
12,736 KB
testcase_16 AC 92 ms
12,616 KB
testcase_17 AC 93 ms
12,612 KB
testcase_18 AC 95 ms
12,616 KB
testcase_19 AC 93 ms
12,720 KB
testcase_20 AC 90 ms
12,744 KB
testcase_21 AC 89 ms
12,736 KB
testcase_22 AC 87 ms
12,612 KB
testcase_23 AC 89 ms
12,740 KB
testcase_24 AC 92 ms
12,708 KB
testcase_25 AC 97 ms
12,612 KB
testcase_26 AC 93 ms
12,656 KB
testcase_27 AC 92 ms
12,736 KB
testcase_28 AC 92 ms
12,744 KB
testcase_29 AC 85 ms
12,584 KB
testcase_30 AC 91 ms
12,612 KB
testcase_31 AC 91 ms
12,712 KB
testcase_32 AC 91 ms
12,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//(A, B)ごとに数えると「平均パス長」だが、Eごとに数えると、Eで分離される2領域のサイズの積を単に足すだけになる。
//これ面白い。ただ、「平均パス長」を聞かれた方が、Eの項が隠される分、線形性が見えにくくなるから、よりテクニカルに見えるかも。
//期待値の線形性の練習としては、この問題の方が適切に見える。
#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;

int powmod(int a, int n, int mod) {
	if (n == 0) return 1;
	if (n % 2 == 1) return a * powmod(a, n - 1, mod) % mod;
	return powmod((a * a) % mod, n / 2, mod);
}

int n;
int from[100000], to[100000];
vector<int> et[100000];
int sub_tree_size[100000];
int parent[100000];

int dfs(int p, int v) {
	int ret = 1;
	
	parent[v] = p;
	for (int i = 0; i < et[v].size(); i++) {
		int nv = et[v][i];
		if (nv == p) continue;
		ret += dfs(v, nv);
	}
	return sub_tree_size[v] = ret;
}

signed main() {
	int i;
	
	cin >> n;
	for (i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v; u--; v--;
		et[u].push_back(v);
		et[v].push_back(u);
		from[i] = u;
		to[i] = v;
	}
	
	dfs(-1, 0);
	
	int setudan = 0;
	for (i = 0; i < n - 1; i++) {
		int u = from[i];
		int v = to[i];
		if (parent[u] == v) swap(u, v);
		int sz = sub_tree_size[v];
		setudan += sz * (n - sz);
	}
	
	int all = n * (n - 1) / 2 * (n - 1);
	
	int mod = 998244353;
	int ans = (all - setudan) % mod * powmod(all % mod, mod - 2, mod) % mod;
	
	cout << ans << endl;
	return 0;
}
0