結果

問題 No.1103 Directed Length Sum
ユーザー FF256grhyFF256grhy
提出日時 2020-07-03 21:54:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 974 ms / 3,000 ms
コード長 2,087 bytes
コンパイル時間 2,332 ms
コンパイル使用メモリ 212,632 KB
実行使用メモリ 124,552 KB
最終ジャッジ日時 2023-10-17 01:52:49
合計ジャッジ時間 12,660 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 749 ms
124,552 KB
testcase_03 AC 483 ms
36,124 KB
testcase_04 AC 556 ms
27,808 KB
testcase_05 AC 974 ms
45,880 KB
testcase_06 AC 348 ms
19,232 KB
testcase_07 AC 70 ms
7,292 KB
testcase_08 AC 116 ms
9,140 KB
testcase_09 AC 44 ms
5,708 KB
testcase_10 AC 160 ms
11,448 KB
testcase_11 AC 602 ms
30,116 KB
testcase_12 AC 351 ms
19,496 KB
testcase_13 AC 167 ms
11,712 KB
testcase_14 AC 31 ms
5,180 KB
testcase_15 AC 269 ms
15,868 KB
testcase_16 AC 685 ms
33,480 KB
testcase_17 AC 725 ms
34,536 KB
testcase_18 AC 159 ms
11,448 KB
testcase_19 AC 622 ms
30,644 KB
testcase_20 AC 52 ms
6,236 KB
testcase_21 AC 104 ms
8,612 KB
testcase_22 AC 514 ms
25,696 KB
testcase_23 AC 290 ms
16,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incID(i, l, r) for(int i = (l)    ; i <  (r); ++i)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); --i)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); ++i)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); --i)
#define inc(i, n)  incID(i, 0, n)
#define dec(i, n)  decID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define FI first
#define SE second
#define FR front()
#define BA back()
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
auto setmin   = [](auto & a, auto b) { return (b <  a ? a = b, true : false); };
auto setmax   = [](auto & a, auto b) { return (b >  a ? a = b, true : false); };
auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); };
#define SI(v) static_cast<int>(v.size())
#define RF(e, v) for(auto & e: v)
#define until(e) while(! (e))
#define if_not(e) if(! (e))
#define ef else if
#define UR assert(false)
#define IN(T, ...) T __VA_ARGS__; IN_(__VA_ARGS__);
void IN_() { };
template<typename T, typename ... U> void IN_(T &  a, U &  ... b) { cin >> a; IN_(b ...); };
template<typename T                > void OUT(T && a            ) { cout << a << endl; }
template<typename T, typename ... U> void OUT(T && a, U && ... b) { cout << a << " "; OUT(b ...); }

// ---- ----

int main() {
	IN(int, n);
	vector<vector<int>> g(n);
	vector<int> c(n);
	inc(i, n - 1) {
		IN(int, a, b);
		a--; b--;
		g[a].PB(b);
		c[b]++;
	}
	set<int> se;
	inc(i, n) { if(c[i] == 0) { se.insert(i); } else { assert(c[i] == 1); } }
	assert(SI(se) == 1);
	int r = * se.begin();
	
	LL ans = 0;
	function<void(int, LL)> dfs = [&](int v, LL h) {
		ans += h * (h + 1) / 2;
		RF(e, g[v]) { dfs(e, h + 1); }
	};
	dfs(r, 0);
	OUT(ans % 1'000'000'007);
}
0