結果

問題 No.100 直列あみだくじ
ユーザー yuusti
提出日時 2015-01-22 14:51:33
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 593 bytes
コンパイル時間 553 ms
コンパイル使用メモリ 60,292 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-24 01:36:32
合計ジャッジ時間 1,764 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int N = 55;
int a[N], f[N];

int dfs(int p, int v){
	if (f[p] >= 0) return f[p] == v;
	f[p] = v;
	if (dfs(v, a[p])) return 1;
	f[p] = -1;
	return 0;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	for (int i = 0; i < n; ++i){
		int x;
		cin >> x;
		a[i] = x - 1;
		f[i] = -1;
	}

	for (int i = 0; i < n; ++i){
		if (f[i] >= 0) continue;
		for (int j = 0; j < n; ++j){
			if (dfs(i, j)) break;
		}
	}

	bool ok = !count(f, f + n, -1);

	cout << (ok ? "Yes" : "No") << endl;
}
0