結果

問題 No.74 貯金箱の退屈
ユーザー hotpepsihotpepsi
提出日時 2016-01-26 23:59:05
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,339 bytes
コンパイル時間 536 ms
コンパイル使用メモリ 61,508 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 16:22:59
合計ジャッジ時間 4,142 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 WA -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 AC 2 ms
4,348 KB
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 2 ms
4,348 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 AC 1 ms
4,348 KB
testcase_27 RE -
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

struct UnionFind {
	std::vector<int> _parent;
	std::vector<int> _size;
	UnionFind(int size) : _parent(size, -1), _size(size, 1) { }
	void unite(int a, int b) {
		int ra = root(a), rb = root(b); if (ra == rb) { return; }
		if (_size[ra] >= _size[rb]) { _parent[rb] = ra, _size[ra] += _size[rb]; } else { _parent[ra] = rb, _size[rb] += _size[ra]; }
	}
	int root(int a) {
		int p = _parent[a];
		if (p < 0) { return a; }
		while (_parent[p] >= 0) { p = _parent[p]; }
		return _parent[a] = p;
	}
	int size(int a) { return _size[root(a)]; }
};

int main(int argc, char *argv[])
{
	int N;
	cin >> N;
	int tot = 0;
	UnionFind uf(N);
	int D[256], W[256], OK[256] = {}, CNT[256] = {};
	for (int i = 0; i < N; ++i) {
		cin >> D[i];
	}
	for (int i = 0; i < N; ++i) {
		cin >> W[i];
		tot += W[i];
	}
	for (int i = 0; i < N; ++i) {
		uf.unite((i - D[i] + N) % N, (i + D[i]) % N);
	}
	for (int i = 0; i < N; ++i) {
		int a = (i - D[i] + N) % N, b = (i + D[i]) % N;
		CNT[uf.root(i)] += W[a] + W[b];
		if (a == b) {
			OK[uf.root(i)] = 1;
		}
	}
	bool ans = true;
	for (int i = 0; i < N; ++i) {
		if (uf.root(i) == i) {
			if (!OK[i] && (uf.size(i) - CNT[i]) % 2) {
				ans = false;
				break;
			}
		}
	}
	if (tot >= N) {
		ans = true;
	}
	cout << (ans ? "Yes" : "No") << endl;
	return 0;
}
0