結果

問題 No.74 貯金箱の退屈
ユーザー koyoprokoyopro
提出日時 2015-10-01 13:17:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,621 bytes
コンパイル時間 1,542 ms
コンパイル使用メモリ 146,680 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 01:12:08
合計ジャッジ時間 2,738 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

#define REP(i, n) for(int i=0; i<(n); i++)
#define FOR(i, a, b) for(int i=(a);i<(b);i++)

int N;
struct coin { int D, W, U, Single; };
vector<coin> coins(1000);

int top(int i) {
    if (coins[i].U < 0) return coins[i].U;
    if (coins[i].U == i) return i;
    return top(coins[i].U);
}

void setu(int i) {
    int bi = (i + coins[i].D) % N;
    int ai = (N*2000 + i - coins[i].D) % N;
    int bg = top(bi);
    int ag = top(ai);
    if (bg < 0 && ag < 0) {
        // 未所属同士: グループ化
        coins[bi].U = coins[ai].U = max(bi, ai);
    } else if (bg < 0){
        // 未所属の方を連結
        coins[bi].U = ag;
    } else if (ag < 0){
        // 未所属の方を連結
        coins[ai].U = bg;
    } else {
        // グループの先頭を連結
        coins[bg].U = ag;
    }
    if (bi == ai) coins[bi].Single = 1;
}

bool isok(int g) {
    int backcnt = 0;
    int singlecnt = 0;
    REP(j,N) {
        if (top(j) != g) continue;
        coin c = coins[j];
        backcnt += (c.W == 0);
        // 一つを返せるやつかどうか
        singlecnt += c.Single;
    }
    return (singlecnt > 0) || (backcnt % 2 == 0);
}

signed main()
{
    cin >> N;
    REP(i,N) cin >> coins[i].D;
    REP(i,N) cin >> coins[i].W;
    REP(i,N) {
        coins[i].U = -1 * (i+1);
        coins[i].Single = 0;
    }
    REP(i,N) {
        // Union-Find木を構築
        setu(i);
    }
    bool ok = true;
    FOR(i,-1*N,N) {
        // Union-Find木毎に判定
        ok &= isok(i);
    }
    cout << (ok ? "Yes" : "No") << endl;
    return 0;
}
0