結果

問題 No.1507 Road Blocked
ユーザー startcppstartcpp
提出日時 2021-05-14 23:05:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,562 bytes
コンパイル時間 874 ms
コンパイル使用メモリ 73,016 KB
実行使用メモリ 18,372 KB
最終ジャッジ日時 2024-04-10 02:27:16
合計ジャッジ時間 5,004 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
7,620 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 -
権限があれば一括ダウンロードができます

ソースコード

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 - 2, mod) % mod;
	
	cout << ans << endl;
	return 0;
}
0