結果

問題 No.1103 Directed Length Sum
ユーザー mamimume____mo
提出日時 2020-07-03 22:53:17
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 956 ms / 3,000 ms
コード長 1,039 bytes
コンパイル時間 841 ms
コンパイル使用メモリ 95,228 KB
実行使用メモリ 99,184 KB
最終ジャッジ日時 2024-09-17 04:34:00
合計ジャッジ時間 9,722 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます
コンパイルメッセージ
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