結果

問題 No.1103 Directed Length Sum
ユーザー QCFiumQCFium
提出日時 2020-07-03 21:42:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 517 ms / 3,000 ms
コード長 579 bytes
コンパイル時間 1,641 ms
コンパイル使用メモリ 177,064 KB
実行使用メモリ 89,532 KB
最終ジャッジ日時 2023-10-17 00:04:59
合計ジャッジ時間 7,374 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 342 ms
89,532 KB
testcase_03 AC 215 ms
32,636 KB
testcase_04 AC 277 ms
25,720 KB
testcase_05 AC 517 ms
42,012 KB
testcase_06 AC 162 ms
17,936 KB
testcase_07 AC 34 ms
6,788 KB
testcase_08 AC 52 ms
8,636 KB
testcase_09 AC 21 ms
5,468 KB
testcase_10 AC 75 ms
10,680 KB
testcase_11 AC 303 ms
27,764 KB
testcase_12 AC 158 ms
18,200 KB
testcase_13 AC 73 ms
10,944 KB
testcase_14 AC 15 ms
4,940 KB
testcase_15 AC 117 ms
14,836 KB
testcase_16 AC 326 ms
30,864 KB
testcase_17 AC 355 ms
31,920 KB
testcase_18 AC 69 ms
10,680 KB
testcase_19 AC 293 ms
28,292 KB
testcase_20 AC 25 ms
5,996 KB
testcase_21 AC 50 ms
8,108 KB
testcase_22 AC 251 ms
23,872 KB
testcase_23 AC 133 ms
15,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

std::vector<std::vector<int> > hen;

int64_t res = 0;
void dfs(int i, int depth) {
	res += (int64_t) depth * (depth + 1) / 2;
	for (auto j : hen[i]) dfs(j, depth + 1);
}

int main() {
	int n = ri();
	hen.resize(n);
	std::vector<bool> used(n);
	for (int i = 1; i < n; i++) {
		int a = ri() - 1;
		int b = ri() - 1;
		hen[a].push_back(b);
		used[b] = true;
	}
	int root = std::find(used.begin(), used.end(), false) - used.begin();
	dfs(root, 0);
	printf("%d\n", (int) (res % 1000000007));
	
	return 0;
}
0