結果

問題 No.1170 Never Want to Walk
ユーザー nawawannawawan
提出日時 2020-08-14 22:36:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,875 bytes
コンパイル時間 844 ms
コンパイル使用メモリ 78,832 KB
実行使用メモリ 13,056 KB
最終ジャッジ日時 2024-04-18 22:26:55
合計ジャッジ時間 7,134 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,752 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 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 4 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 9 ms
5,376 KB
testcase_23 AC 5 ms
5,376 KB
testcase_24 AC 9 ms
5,376 KB
testcase_25 AC 10 ms
5,376 KB
testcase_26 AC 7 ms
5,376 KB
testcase_27 AC 353 ms
7,168 KB
testcase_28 AC 355 ms
7,296 KB
testcase_29 AC 364 ms
7,040 KB
testcase_30 AC 362 ms
7,296 KB
testcase_31 AC 362 ms
7,168 KB
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    //UnionFind(int n) { init(n); };
    void init(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)];
    }
};
UnionFind U;
vector<int> used;
void dfs(int pre, int s){
    if(used[s] != 0) {
        if(pre == -1) return;
        U.unite(pre, s);
        return;
    }
    used[s] = 1;
    int ze = lower_bound(x.begin(), x.end(), x[s] + A) - x.begin();
    int g = upper_bound(x.begin(), x.end(), x[s] + B) - x.begin();
    long long res = 1;
    for(int i = ze; i < g; i++){
        U.unite(s, i);
        dfs(s, i);
    }
}
int main(){
    long long N;
    cin >> N >> A >> B;
    x.resize(N);
    used.resize(N, 0);
    U.init(N);
    for(int i = 0; i < N; i++){
        cin >> x[i];
    }
    for(int i = 0; i < N; i++){
        dfs(-1, i);
    }
    for(int i = 0; i < N; i++){
        cout << U.size(i) << endl;
    }
}
0