結果

問題 No.30 たこやき工場
ユーザー ty70ty70
提出日時 2015-06-25 05:17:29
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,166 bytes
コンパイル時間 789 ms
コンパイル使用メモリ 94,948 KB
実行使用メモリ 7,512 KB
最終ジャッジ日時 2023-09-22 00:37:11
合計ジャッジ時間 7,823 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define INF 1500*100*10
/*
	No.30 たこやき工場

	深さ優先探索

	Pi から Ri を作るのに Pi が Qi 個必要というグラフを

	Pi <- Qi - Ri と言う逆のグラフを作る。

	N から 順にグラフを辿って葉になったら、その時点での個数 num を記録し、そのノードが葉であることを明示する。
	葉でない場合、Σ (num)x(子のコスト) が 現在のノードのコストとなる
*/
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int MAX_N = 105;
vector<P> G[MAX_N];	// P (ind, cost )

int memo[MAX_N];
bool is_leaf[MAX_N];

int dfs (int curr, int num ){

	int ans = 0;

	if (G[curr].empty() ){
		is_leaf[curr] |= true;
		ans = num;
	}else{
		rep (i, G[curr].size() ){
			int to = G[curr][i].first;
			int cost = G[curr][i].second;
			ans += dfs (to, num*cost );
		} // end rep
	} // end if

	return memo[curr] += ans;
}

int main()
{
	memset (memo, 0, sizeof (memo ) );
	memset (is_leaf, false, sizeof (is_leaf ) );
	rep (i, MAX_N ) G[i].clear();
	ios_base::sync_with_stdio(0);
	int N, M; cin >> N >> M;
	rep (i, M ){
		int p, q, r; cin >> p >> q >> r;
		G[r].push_back (P (p, q ) );
	} // end rep

	rep (i, MAX_N ){
		if (!G[i].empty() ) sort (ALL (G[i] ) );
	} // end rep

	dfs (N, 1 );

	for (int i = 1; i < N; i++ ){
		cout << (is_leaf[i] ? memo[i] : 0 ) << endl;
	} // end for
	return 0;
}
0