結果

問題 No.74 貯金箱の退屈
ユーザー data9824
提出日時 2015-06-22 01:46:24
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,175 bytes
コンパイル時間 877 ms
コンパイル使用メモリ 71,956 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-07 16:11:45
合計ジャッジ時間 1,898 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

int n;
vector<int> w;
vector<vector<bool> > connected;
vector<bool> visited;

int countBack(int node) {
	if (visited[node]) {
		return 0;
	}
	visited[node] = true;
	int result = (w[node] == 0) ? 1 : 0;
	bool loopFound = (connected[node][node]);
	for (int i = 0; i < n; ++i) {
		if (connected[node][i]) {
			int count = countBack(i);
			if (count == -1) {
				loopFound = true;
			} else {
				result += count;
			}
		}
	}
	return loopFound ? -1 : result;
}

int main() {
	cin >> n;
	vector<int> d(n);
	for (int i = 0; i < n; ++i) {
		cin >> d[i];
	}
	w.resize(n);
	for (int i = 0; i < n; ++i) {
		cin >> w[i];
	}
	connected.assign(n, vector<bool>(n));
	visited.assign(n, false);
	for (int i = 0; i < n; ++i) {
		int dist = d[i] % n;
		int node1 = (i + dist) % n;
		int node2 = (i + n - dist) % n;
		connected[node1][node2] = true;
		connected[node2][node1] = true;
	}
	bool possible = true;
	for (int i = 0; i < n; ++i) {
		if (!visited[i]) {
			int backs = countBack(i);
			if (backs != -1 && backs % 2 == 1) {
				possible = false;
				break;
			}
		}
	}
	cout << (possible ? "Yes" : "No") << endl;
	return 0;
}
0