結果

問題 No.1103 Directed Length Sum
ユーザー mamimume____momamimume____mo
提出日時 2020-07-03 22:53:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 810 ms / 3,000 ms
コード長 1,039 bytes
コンパイル時間 798 ms
コンパイル使用メモリ 94,964 KB
実行使用メモリ 99,268 KB
最終ジャッジ日時 2023-10-17 05:55:45
合計ジャッジ時間 8,944 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 628 ms
99,268 KB
testcase_03 AC 425 ms
43,676 KB
testcase_04 AC 433 ms
32,228 KB
testcase_05 AC 810 ms
53,332 KB
testcase_06 AC 251 ms
22,136 KB
testcase_07 AC 54 ms
7,820 KB
testcase_08 AC 85 ms
10,196 KB
testcase_09 AC 34 ms
6,236 KB
testcase_10 AC 122 ms
12,768 KB
testcase_11 AC 469 ms
34,800 KB
testcase_12 AC 261 ms
22,400 KB
testcase_13 AC 133 ms
13,032 KB
testcase_14 AC 28 ms
5,456 KB
testcase_15 AC 194 ms
18,244 KB
testcase_16 AC 534 ms
38,624 KB
testcase_17 AC 613 ms
40,208 KB
testcase_18 AC 123 ms
12,768 KB
testcase_19 AC 490 ms
35,524 KB
testcase_20 AC 42 ms
6,764 KB
testcase_21 AC 76 ms
9,668 KB
testcase_22 AC 383 ms
29,588 KB
testcase_23 AC 207 ms
19,232 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:52:22: warning: 's' may be used uninitialized [-Wmaybe-uninitialized]
   52 |         for(int v:e[s]){
      |                      ^
main.cpp:47:13: note: 's' was declared here
   47 |         int s;
      |             ^

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <deque>
#include <string>
#include <stack>
#include <vector>
#include <set>
#include <tuple>
#include <utility>
#include <functional>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
const int INF = 1000000000;
const int MOD = 1000000007;
vector<vector<int>> e;
vector<ll> len;

void solve(int par,ll path_len,int now){
	//path_len=根からの距離.
	len[now] = path_len;
	for(int next:e[now]){
		solve(now,path_len+1,next);
	}
}

int main(){
	int n;
	cin >> n;
	e.resize(n);
	len.resize(n,0);
	vector<int> in_deg(n,0);
	for(int i = 0;i < n-1;i++){
		int a,b;
		cin >> a >> b;
		a--,b--;
		e[a].push_back(b);
		in_deg[b]++;
	}

	int s;
	for(int i = 0;i < n;i++){
		if(in_deg[i] == 0)s = i;
	}

	for(int v:e[s]){
		solve(s,1LL,v);
	}

	ll ans = 0;
	for(int i = 0;i < n;i++){
		ans += (len[i] + 1) * len[i] / 2;
		ans %= MOD;
	}

	cout << ans << endl;
}
0