結果

問題 No.1103 Directed Length Sum
ユーザー 夜
提出日時 2020-11-28 09:46:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 879 ms / 3,000 ms
コード長 916 bytes
コンパイル時間 1,181 ms
コンパイル使用メモリ 102,328 KB
実行使用メモリ 58,588 KB
最終ジャッジ日時 2023-10-09 23:40:36
合計ジャッジ時間 10,040 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
26,488 KB
testcase_01 AC 11 ms
26,160 KB
testcase_02 AC 656 ms
58,588 KB
testcase_03 AC 458 ms
47,876 KB
testcase_04 AC 504 ms
37,516 KB
testcase_05 AC 879 ms
45,280 KB
testcase_06 AC 309 ms
33,368 KB
testcase_07 AC 70 ms
28,088 KB
testcase_08 AC 109 ms
29,020 KB
testcase_09 AC 46 ms
27,672 KB
testcase_10 AC 147 ms
30,148 KB
testcase_11 AC 538 ms
38,228 KB
testcase_12 AC 308 ms
33,516 KB
testcase_13 AC 152 ms
30,116 KB
testcase_14 AC 36 ms
27,388 KB
testcase_15 AC 237 ms
32,052 KB
testcase_16 AC 607 ms
39,732 KB
testcase_17 AC 635 ms
40,116 KB
testcase_18 AC 152 ms
30,052 KB
testcase_19 AC 556 ms
38,396 KB
testcase_20 AC 57 ms
27,596 KB
testcase_21 AC 100 ms
28,684 KB
testcase_22 AC 443 ms
36,352 KB
testcase_23 AC 256 ms
32,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
vector<vector<int>> vec(1000010);
bool bo[1000010] = { false };
int main() {
	long long n;
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int a, b;
		cin >> a >> b;
		vec[a].emplace_back(b);
		bo[b] = true;
	}
	queue<pair<int, long long>> que;
	for (int i = 1; i <= n; i++) {
		if (!bo[i]) {
			que.push(make_pair(i, 0));
		}
	}
	long long ans = 0;
	while (!que.empty()) {
		pair<int, long long> p;
		p = que.front();
		ans += p.second * (p.second + 1) / 2;
		ans %= 1000000007;
		for (int i = 0; i < vec[p.first].size(); i++) {
			que.push(make_pair(vec[p.first][i], p.second + 1));
		}
		que.pop();
	}
	cout << ans << endl;
}
0