結果

問題 No.1170 Never Want to Walk
ユーザー nawawannawawan
提出日時 2020-08-14 22:09:23
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 2,042 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 82,680 KB
実行使用メモリ 821,584 KB
最終ジャッジ日時 2024-10-10 15:26:12
合計ジャッジ時間 7,317 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30 MLE * 1 -- * 6
権限があれば一括ダウンロードができます

ソースコード

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