結果

問題 No.1103 Directed Length Sum
ユーザー forest3forest3
提出日時 2020-07-04 22:23:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,083 ms / 3,000 ms
コード長 884 bytes
コンパイル時間 1,694 ms
コンパイル使用メモリ 181,672 KB
実行使用メモリ 69,632 KB
最終ジャッジ日時 2024-09-19 13:55:31
合計ジャッジ時間 13,560 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 672 ms
69,632 KB
testcase_03 AC 465 ms
50,464 KB
testcase_04 AC 548 ms
32,640 KB
testcase_05 AC 1,083 ms
54,144 KB
testcase_06 AC 312 ms
22,400 KB
testcase_07 AC 60 ms
7,808 KB
testcase_08 AC 96 ms
10,240 KB
testcase_09 AC 38 ms
6,144 KB
testcase_10 AC 137 ms
12,928 KB
testcase_11 AC 622 ms
35,328 KB
testcase_12 AC 329 ms
22,528 KB
testcase_13 AC 151 ms
13,184 KB
testcase_14 AC 28 ms
5,376 KB
testcase_15 AC 245 ms
18,304 KB
testcase_16 AC 726 ms
39,296 KB
testcase_17 AC 746 ms
40,832 KB
testcase_18 AC 134 ms
12,928 KB
testcase_19 AC 627 ms
36,096 KB
testcase_20 AC 45 ms
6,784 KB
testcase_21 AC 82 ms
9,728 KB
testcase_22 AC 510 ms
30,080 KB
testcase_23 AC 265 ms
19,328 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/allocator.h:46,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/string:41,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/locale_classes.h:40,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/ios_base.h:41,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ios:42,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/istream:38,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/sstream:38,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/complex:45,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ccomplex:39,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/stdc++.h:54,
                 from main.cpp:1:
In member function 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = int; _Args = {const int&}; _Tp = int]',
    inlined from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = int; _Args = {const int&}; _Tp = int]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/alloc_traits.h:516:17,
    inlined from 'void std::deque<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator<int>]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_deque.h:1543:30,
    inlined from 'void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = int; _Sequence = std::deque<int, std::allocator<int> >]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int N;;
	cin >> N;
	vector<int> in( N );
	vector<vector<int>> g( N );
	for( int i = 0; i < N - 1; i++ ) {
		int A, B;
		cin >> A >> B;
		A--;
		B--;
		g[A].push_back( B );
		in[B]++;
	}

	int r;
	for( int i = 0; i < N; i++ ) {
		if( in[i] == 0 ) r = i;
	}
	vector<long long> l( N );
	typedef pair<int, int> P;
	queue<P> que;
	que.push( P( r, 0 ) );
	while( !que.empty() ) {
		int i, len;
		tie( i, len ) = que.front();
		que.pop();
		l[i] = len;
		for( int e : g[i] ) {
			que.push( P(e, len + 1) );
		}
	}
	const long long MOD = 1000000000 + 7;
	long long ans = 0;
	queue<int> que1;
	que1.push( r );
	while( !que1.empty() ) {
		int i = que1.front();
		long long a = l[i];
		que1.pop();
		for( int e : g[i] ) {
			que1.push( e );
			l[e] += a;
			l[e] %= MOD;
			ans += l[e];
			ans %= MOD;
		}
	}

	cout << ans << endl;
}
0