結果

問題 No.1103 Directed Length Sum
ユーザー takumattakumat
提出日時 2020-07-04 19:55:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,519 bytes
コンパイル時間 1,140 ms
コンパイル使用メモリ 111,552 KB
実行使用メモリ 151,824 KB
最終ジャッジ日時 2023-10-19 12:53:43
合計ジャッジ時間 21,790 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 AC 950 ms
73,624 KB
testcase_04 AC 1,274 ms
43,124 KB
testcase_05 AC 2,420 ms
71,776 KB
testcase_06 AC 722 ms
29,268 KB
testcase_07 AC 122 ms
9,624 KB
testcase_08 AC 225 ms
12,792 KB
testcase_09 AC 71 ms
7,248 KB
testcase_10 AC 313 ms
16,156 KB
testcase_11 AC 1,480 ms
46,440 KB
testcase_12 AC 775 ms
29,220 KB
testcase_13 AC 337 ms
16,684 KB
testcase_14 AC 53 ms
6,192 KB
testcase_15 AC 574 ms
23,480 KB
testcase_16 AC 1,615 ms
51,916 KB
testcase_17 AC 1,709 ms
54,028 KB
testcase_18 AC 306 ms
16,156 KB
testcase_19 AC 1,483 ms
47,808 KB
testcase_20 AC 98 ms
8,040 KB
testcase_21 AC 193 ms
12,000 KB
testcase_22 AC 1,158 ms
39,692 KB
testcase_23 AC 625 ms
25,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>

typedef unsigned long long ULLONG;
typedef long long LLONG;
static const LLONG MOD_NUM = 1000000007;//998244353;

template<class _T> static void getval(_T& a) {
	std::cin >> a;
}
template<class _T> static void getval(_T& a, _T& b) {
	std::cin >> a >> b;
}
template<class _T> static void getval(_T& a, _T& b, _T& c) {
	std::cin >> a >> b >> c;
}

static void exec();

int main()
{
	exec();
	fflush(stdout);
	return 0;
}

static LLONG dfs(std::vector<std::vector<int>>& graph, int here, int pass)
{
	LLONG ans = 0;
	int size = 0;
	if ((size = graph[here].size()) == 0) {
		ans = (pass * (pass + 1) / 2LL) % MOD_NUM;
	}
	else {
		for (auto it = graph[here].begin(); it != graph[here].end(); it++) {
			ans = (ans + dfs(graph, *it, pass + 1)) % MOD_NUM;
		}
		ans += (pass * (pass + 1) / 2LL) % MOD_NUM;
		ans %= MOD_NUM;
	}
	return ans;
}

static void exec()
{
	int N;
	getval(N);

	std::set<int> vers;
	for (int i = 0; i < N; i++) {
		vers.insert(i);
	}
	std::vector<std::vector<int>> graph(N);
	for (int i = 1; i < N; i++) {
		int a, b;
		getval(a, b); a--; b--;
		graph[a].push_back(b);
		vers.erase(b);
	}

	int root = *(vers.begin());
	LLONG ans = 0;
	for (auto it = graph[root].begin(); it != graph[root].end(); it++) {
		ans += dfs(graph, *it, 1);
		ans %= MOD_NUM;
	}
	printf("%lld\n", ans);
}
0