結果

問題 No.1170 Never Want to Walk
ユーザー nawawannawawan
提出日時 2020-08-14 22:09:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 2,042 bytes
コンパイル時間 977 ms
コンパイル使用メモリ 83,128 KB
実行使用メモリ 823,712 KB
最終ジャッジ日時 2024-04-18 21:58:06
合計ジャッジ時間 6,949 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 7 ms
5,376 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 7 ms
5,376 KB
testcase_25 AC 7 ms
5,376 KB
testcase_26 AC 6 ms
5,376 KB
testcase_27 AC 328 ms
8,448 KB
testcase_28 AC 327 ms
8,448 KB
testcase_29 AC 330 ms
8,576 KB
testcase_30 AC 336 ms
8,576 KB
testcase_31 AC 336 ms
8,576 KB
testcase_32 MLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
struct UnionFind{
    vector<int> par;//親(根)
    vector<int> rank;//木の深さ

    //UnionFind(int n) { init(n); };
    UnionFind(int n){//初期化関数
        par.resize(n);
        rank.resize(n);
        for(int i = 0; i < n; i++){
            par[i] = i;//初めはノード一個の木なので根は自身
            rank[i] = 1;
        }
    }

    int root(int x){//木の根を求める
    if(par[x] == x) return x;
    else return par[x] = root(par[x]);
    }

    bool same(int x, int y){//同じ木かどうか判定
        return root(x) == root(y);//同じ木ならtrue
    }

    void unite(int x, int y){
        x = root(x);
        y = root(y);
        if(x == y) return;//もし同じ木に属していたら何もしない
        if(rank[x] < rank[y]) swap(x, y);//数が大きい方に小さい方を結合させる
        par[y] = x;
        rank[x] += rank[y];//深さが同じ時だけ結合後深さが増える
    }

    int size(int x) {//深さを返す関数
        return rank[root(x)];
    }
};
int main(){
    long long N, A, B;
    cin >> N >> A >> B;
    vector<long long> x(N), y(N);
    for(int i = 0; i < N; i++){
        cin >> x[i];
        y[i] = x[i];
    }
    UnionFind U(N);
    vector<int> used(N, 0);
    for(int i = 0; i < N; i++){
        if(used[i] == 0){
            queue<int> q;
            q.push(i);
            while(!q.empty()){
                int v = q.front();
                q.pop();
                if(used[v] == 1) continue;
                used[v] = 1;
                int ze = lower_bound(x.begin(), x.end(), A + x[v]) - x.begin();
                int g = upper_bound(x.begin(), x.end(), x[v] + B) - x.begin();
                for(int j = ze; j < g; j++) {
                    U.unite(v, j);
                    if(used[j] == 0) q.push(j);
                }
            }
        }
    }
    for(int i = 0; i < N; i++){
        cout << U.size(i) << endl;
    }
}
0