結果

問題 No.74 貯金箱の退屈
ユーザー codershifthcodershifth
提出日時 2015-08-02 09:58:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,613 bytes
コンパイル時間 1,364 ms
コンパイル使用メモリ 153,900 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-25 00:31:07
合計ジャッジ時間 2,644 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;

class UFTree {
    std::vector<int> par_;
    std::vector<int> rank_;
public:
    UFTree(int N) { init(N); }
    UFTree() {}
    ~UFTree() {}

    void init(int N) {
        par_.resize(N);
        rank_.resize(N, 0);
        for (int i = 0; i < N; ++i)
            par_[i] = i;
    }
    int find(int x) {
        if ( par_[x] == x )
            return x;
        else
            return ( par_[x] = find(par_[x]));
    }
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if ( x == y )
            return;
        if ( rank_[x] < rank_[y] )
            par_[x] = y;
        else
        {
            par_[y] = x;
            if ( rank_[x] == rank_[y] )
                ++rank_[x];
        }
    }
    bool same(int x, int y) {
        return (find(x) == find(y));
    }
    int operator[](size_t i) { return find(i); }
};

class BoredomOfSavingsBox {
public:
    void solve(void) {
            int N;
            cin>>N;

            //
            // 同時にひっくり返せるものを一つのグループとして考える。
            //
            // 0,1,1,0,1,...
            //
            // これは適当にコインをひっくりかえすことで
            //
            // A. 1,1,1,...,1,1
            // B. 1,1,1,,..,1,0
            //
            // のどちらかのケースに帰着できる
            // (以下のように反転していけるから
            // 0,1,* -> 1,0,*
            // 0,0,* -> 1,1,*
            // 1,0,1 -> 1,1,0
            // 1,0,0 -> 1,1,1
            // )
            //
            // 一度に二個のコインをひっくり返すので
            // 裏になっているコインの数の偶奇は変化しない。
            // よって初期状態の裏のコインの数が奇数のときは全て表にはできない。
            //
            // ただし1個だけコインをひっくり返せる場合はそれで帳尻があうので全て表にできる。
            //
            UFTree uft(N);
            vector<bool> one(N,false);
            REP(i,N)
            {
                int d;
                cin>>d;
                int l = (1000*N+i-d)%N;
                int r = (d+i)%N;
                // 同時にひっくり返せるものを統合する
                uft.unite(l,r);
                if (l == r)
                    one[i] = true;
            }
            // 各連結成分での偶奇チェック
            vector<int> num(N,0);
            REP(i,N)
            {
                int w;
                cin>>w;
                num[uft[i]] += (w==0); // 裏になっているコインの数を数える
                // 各 one の結果を連結成分に適用
                if (one[i])
                    one[uft[i]] = true;
            }
            // 各連結成分ごとにチェック
            REP(i,N)
            {
                if (uft[i] != i) // 連結成分ごとにチェック
                    continue;
                if (num[uft[i]]%2 && !one[uft[i]])
                {
                    cout<<"No"<<endl;
                    return;
                }
            }
            cout<<"Yes"<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new BoredomOfSavingsBox();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0