結果

問題 No.74 貯金箱の退屈
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-08-26 20:53:59
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,459 bytes
コンパイル時間 844 ms
コンパイル使用メモリ 74,164 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-25 18:03:58
合計ジャッジ時間 2,034 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 WA -
testcase_02 AC 2 ms
6,940 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 1 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 1 ms
6,944 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,944 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];
        // d[i] %= n;
        // printf("%d  ", d[i]);
    }
    // printf("\n");
    rep(i, n) cin >> w[i];
    vector<bool> flag(n, 0);//1つだけで反転できるやつはtrue

    vector<pair<int, int> > memo;
    for (int i = 0; i < n; ++i){
        int l = (i - d[i] + 1000 * n) % n;
        int r = (i + d[i]) % n;
        // printf("%d %d\n", l, r);
        memo.push_back(make_pair(l, r));
        if(l == r) flag[l] = true;
    }

    for(auto v : memo){
        if(v.first == v.second) continue;
        if(flag[v.first]) continue;
        if(flag[v.second]) continue;
        ds.unite(v.first, v.second);
    }

    vector<int> parentura(n, 0); //根がiのもので始めに裏が奇数のものの数
    for (int i = 0; i < n; ++i){
        if(flag[i])continue;
        int ret = ds.findSet(i);
        if(w[ret] == 0) parentura[ret]++;
    }
    rep(i, n){
        if(parentura[i] % 2 != 0){
            printf("No\n");
            return 0;
        }
    }
    printf("Yes\n");
    return 0;
}
0