結果

問題 No.1103 Directed Length Sum
ユーザー 夜
提出日時 2020-11-28 09:45:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 895 bytes
コンパイル時間 845 ms
コンパイル使用メモリ 100,892 KB
実行使用メモリ 58,844 KB
最終ジャッジ日時 2023-10-09 23:40:24
合計ジャッジ時間 11,559 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
26,144 KB
testcase_01 AC 7 ms
26,360 KB
testcase_02 WA -
testcase_03 AC 421 ms
47,712 KB
testcase_04 AC 443 ms
37,608 KB
testcase_05 AC 789 ms
45,176 KB
testcase_06 AC 261 ms
33,520 KB
testcase_07 AC 60 ms
28,168 KB
testcase_08 AC 91 ms
29,028 KB
testcase_09 AC 44 ms
27,616 KB
testcase_10 AC 129 ms
30,136 KB
testcase_11 AC 453 ms
38,168 KB
testcase_12 AC 275 ms
33,496 KB
testcase_13 AC 138 ms
30,052 KB
testcase_14 AC 38 ms
27,216 KB
testcase_15 AC 216 ms
32,108 KB
testcase_16 AC 553 ms
39,772 KB
testcase_17 AC 570 ms
40,160 KB
testcase_18 AC 129 ms
29,988 KB
testcase_19 AC 500 ms
38,460 KB
testcase_20 AC 50 ms
27,796 KB
testcase_21 AC 88 ms
28,772 KB
testcase_22 AC 404 ms
36,632 KB
testcase_23 AC 210 ms
32,460 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;
		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