結果

問題 No.74 貯金箱の退屈
ユーザー hotpepsihotpepsi
提出日時 2016-01-27 00:20:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,409 bytes
コンパイル時間 492 ms
コンパイル使用メモリ 61,604 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 16:23:12
合計ジャッジ時間 1,518 ms
ジャッジサーバーID
(参考情報)
judge9 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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) {
		int a = (i - (D[i] % N) + N) % N, b = (i + D[i]) % N;
		uf.unite(a, b);
	}
	for (int i = 0; i < N; ++i) {
		int a = (i - (D[i] % N) + N) % N, b = (i + D[i]) % N;
		if (a == b) {
			OK[uf.root(i)] = 1;
		}
		CNT[uf.root(i)] += W[i];
	}
	bool ans = true;
	for (int i = 0; i < N; ++i) {
		if (uf.root(i) == i) {
			int sz = uf.size(i);
			int cnt = CNT[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