結果

問題 No.1103 Directed Length Sum
ユーザー takumat
提出日時 2020-07-04 19:55:40
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,519 bytes
コンパイル時間 1,094 ms
コンパイル使用メモリ 107,832 KB
最終ジャッジ日時 2025-01-11 15:43:43
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21 WA * 1
権限があれば一括ダウンロードができます

ソースコード

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