結果

問題 No.30 たこやき工場
ユーザー furonfuron
提出日時 2023-06-01 23:50:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,936 bytes
コンパイル時間 1,292 ms
コンパイル使用メモリ 131,688 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-08-28 02:12:54
合計ジャッジ時間 2,095 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <string>
#include <queue>
#include <map>
#include <bitset>
#include <set>
#include <stack>
#include <numeric>
#include <unordered_map>
#include <random>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using vpdd = vector<pdd>;
const int inf = (1 << 30) - 1;
const ll INF = 1LL << 60;
//const int MOD = 1000000007;
const int MOD = 998244353;

struct Edge {
	int to;
	ll cost;
};

using Graph = vector<vector<Edge>>;
vl memo;
ll dfs(int s, Graph& g) {
	if (memo[s]) return memo[s];

	ll ret = 0;
	
	for (auto& v : g[s]) {
		ll x = dfs(v.to, g);
		ret += x * v.cost;
	}
	return memo[s] = ret;
}

int main() {
	int n, m;
	cin >> n >> m;
	vi p(m), q(m), r(m);
	for (int i = 0; i < m; i++) {
		cin >> p[i] >> q[i] >> r[i];
	}
	vl ans(n + 1, 0);
	Graph g(n + 1);
	// 辺を逆向きにして製品を作るのに必要な材料の数をDFSで求める方法はTLE
	// 大元の材料が次の材料に使われる数をメモ化再帰で求める
	for (int i = 0; i < m; i++) {
		g[p[i]].push_back({ r[i], q[i] });
	}

	// 頂点の入次数を求める
	vi indeg(n + 1, 0);
	for (int i = 0; i < m; i++) {
		indeg[r[i]]++;
	}
	// memo[i]: 材料iが必要な数を保存
	// 頂点N = 1 から逆向きに決まる
	memo.assign(n + 1, 0);
	memo[n] = 1;

	// 入次数 0 の頂点からDFSする
	for (int i = 1; i < n; i++) {
		if (indeg[i] == 0) dfs(i, g);
	}

	for (int i = 1; i < n; i++) {
		if (indeg[i] == 0) cout << memo[i] << endl;
		else cout << 0 << endl;
	}

	return 0;
}
0