結果

問題 No.74 貯金箱の退屈
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-08-26 21:22:34
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,195 bytes
コンパイル時間 762 ms
コンパイル使用メモリ 70,880 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-25 18:21:10
合計ジャッジ時間 1,759 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);i++)

class DisjointSet{
    public:
        vector<int> rank, p;//rank:木の高さ p:親の頂点番号
        DisjointSet(){}
        DisjointSet(int size){//頂点の数
            rank.resize(size, 0);
            p.resize(size, 0);
            rep(i, size) makeSet(i);
        }
        void makeSet(int x){
            p[x] = x;
            rank[x] = 0;
        }
        //同じ木にあるか判定(今回使わない)
        bool same(int x, int y){
            return findSet(x) == findSet(y);
        }
        // 木どうしをくっつける
        void unite(int x, int y){
            link(findSet(x), findSet(y));
        }
        //木の高さを考慮して木どうしをくっつける
        void link(int x, int y){
            if(rank[x] > rank[y]){
                p[y] = x;
            }else{
                p[x] = y;
                if(rank[x] == rank[y]) rank[y]++;
            }
        }
        //親を探す(ルートまで)
        int findSet(int x){
            if(x != p[x]){
                p[x] = findSet(p[x]);
            }
            return p[x];
        }
};

int main(void){
    int n; cin >> n;
    DisjointSet ds = DisjointSet(n);
    vector<int> d(n);
    vector<int> w(n);
    rep(i, n) cin >> d[i];
    rep(i, n) cin >> w[i];

    vector<bool> flag(n, false);//1つだけで反転できるやつはtrue
    for (int i = 0; i < n; ++i){
        int l = (i - d[i] + 1000 * n) % n;
        int r = (i + d[i]) % n;
        ds.unite(l, r);
        if(l == r) flag[l] = true;
    }

    vector<int> ura(n, 0); //根がiのもので初めに裏が奇数のものの数
    vector<bool> iti(n, false); //根がiで一枚で反転できやつが存在するか
    for (int i = 0; i < n; ++i){
        int ret = ds.findSet(i);
        if(w[ret] == 0) ura[ret]++;
        if(flag[ret]) iti[ret] = true;
    }
    rep(i, n){
        if(ura[i] % 2 != 0 && !iti[i]){
            printf("No\n");
            return 0;
        }
    }
    printf("Yes\n");
    return 0;
}
0