結果

問題 No.74 貯金箱の退屈
ユーザー koyoprokoyopro
提出日時 2015-10-01 13:06:58
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,589 bytes
コンパイル時間 1,464 ms
コンパイル使用メモリ 146,960 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-27 01:11:24
合計ジャッジ時間 2,304 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

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

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

int top(int i) {
    if (coins[i].U == -1) return -1;
    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 == -1 && ag == -1) {
        // 未所属同士: グループ化
        coins[bi].U = coins[ai].U = max(bi, ai);
    } else {
        coins[bi].U = coins[ai].U = max(bg, ag); // -1以外でグループに入る
    }
    if (bi == ai) coins[bi].Single = coins[ai].Single = 1;
}

bool isok(int i) {
    int backcnt = 0;
    int singlecnt = 0;
    int u = -1;
    REP(j,N) {
        coin c = coins[j];
        if (u == -1) {
            u = top(j);
        } else {
            if (top(j) != u) continue;
        }
        backcnt += (c.W == 0);
        // 一つを返せるやつかどうか
        singlecnt += c.Single;
        if (u == -1) break; // 未所属なのでチェック終わり
    }
    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;
        coins[i].Single = 0;
    }
    REP(i,N) {
        // Union-Find木を構築
        setu(i);
    }
    bool ok = true;
    REP(i,N) {
        // Union-Find木毎に判定
        ok &= isok(i);
    }
    cout << (ok ? "Yes" : "No") << endl;
    return 0;
}
0