結果

問題 No.30 たこやき工場
ユーザー finefine
提出日時 2017-02-27 20:38:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,702 bytes
コンパイル時間 1,554 ms
コンパイル使用メモリ 171,516 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-02 13:07:43
合計ジャッジ時間 2,495 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

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

int ans[100];
bool par[100];
vector<P> g[100];

ll dfs(int cur) {
	if (ans[cur] != 0) return ans[cur];
	if (g[cur].empty()) return 1;
	ll res = 0;
	for (P p : g[cur]) {
		res += dfs(p.second) * p.first;
	}
	ans[cur] = res;
	return res;
}

struct Union_Find {
    //各要素が属する集合の代表(根)を管理する
    //もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい
    vector<int> data;
    
    Union_Find(int size) : data(size, -1) {}
    bool Union(int x, int y) {
        x = Find(x);
        y = Find(y);
        bool is_union = (x != y);
        if (is_union) {
            if (data[x] > data[y]) swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
        return is_union;
    }
    int Find(int x) {
        if (data[x] < 0) { //要素xが根である
            return x;
        } else {
            data[x] = Find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新される
            return data[x];
        }
    }
    bool same(int x, int y) {
        return Find(x) == Find(y);
    }
    int size(int x) {
        return -data[Find(x)];
    }
};


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	Union_Find uf(n);
	for (int i = 0; i < m; i++) {
		int p, r;
		ll q;
		cin >> p >> q >> r;
		p--;
		r--;
		g[p].emplace_back(q, r);
		par[r] = true;
		uf.Union(p, r);
	}
	
	for (int i = 0; i < n - 1; i++) {
		if (par[i] || !uf.same(i, n - 1)) cout << 0 << endl;
		else cout << dfs(i) << endl;
	}
	return 0;
}
0