Submission #18983198


Source Code Expand

Copy
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}

const int MAX_N = 110100;
int par[MAX_N]; // 親
int rank_[MAX_N]; // 木の深さ
int cnt_[MAX_N]; // 属する頂点の個数(親のみ正しい)

// n要素で初期化
void UFinit(){
    for(int i=0;i<MAX_N;i++){
        par[i] = i;
        rank_[i] = 0;
        cnt_[i] = 1;
    }
}

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

// xとyの属する集合を併合
void unite(int x, int y){
    x = find(x);
    y = find(y);
    if(x == y) return;

    if(rank_[x] < rank_[y]){
        par[x] = y;
        cnt_[y] += cnt_[x];
    }else{
        par[y] = x;
        cnt_[x] += cnt_[y];
        if(rank_[x] == rank_[y]) rank_[x]++;
    }
}

// xとyが同じ集合に属するか否か
bool same(int x, int y){
    return find(x) == find(y);
}


int main() {
    UFinit();
    int n,m;
    cin >> n >> m;
    int a[m], b[m];
    rep(i,m){
        cin >> a[i] >> b[i];
        a[i]--; b[i]--;
    }
    int q;
    cin >> q;
    int x[q], y[q];
    rep(i,q){
        cin >> x[i] >> y[i];
        x[i]--; y[i]--;
    }
    int l[q], r[q];
    rep(i,q){
        l[i] = 0;
        r[i] = m;
    }

    // -1判定
    rep(i,m){
        unite(a[i],b[i]);
    }
    rep(i,q){
        if(same(x[i],y[i]) == 0) l[i] = m;
    }

    rep(_,50){
        UFinit();
        vector<int> mid[m+10];
        rep(i,q) mid[(l[i]+r[i])/2].push_back(i);

        rep(i,m){
            unite(a[i],b[i]);
            rep(j,mid[i].size()){
                if(same(x[mid[i][j]], y[mid[i][j]])){
                    r[mid[i][j]] = i;
                }else{
                    l[mid[i][j]] = i;
                }
            }
        }
    }

    rep(i,q){
        int ans = -1;
        if(l[i] != m) ans = r[i]+1;
        cout << ans << endl; 
    }
    return 0;
}

Submission Info

Submission Time
Task H - Union Sets
User Shibuyap
Language C++ (GCC 9.2.1)
Score 600
Code Size 2234 Byte
Status AC
Exec Time 618 ms
Memory 11236 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:2:35: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
    2 | #define rep(i,n) for(int i = 0; i < (n); ++i)
      |                                   ^
./Main.cpp:92:13: note: in expansion of macro ‘rep’
   92 |             rep(j,mid[i].size()){
      |             ^~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 49
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_18.txt, subtask_1_19.txt, subtask_1_2.txt, subtask_1_20.txt, subtask_1_21.txt, subtask_1_22.txt, subtask_1_23.txt, subtask_1_24.txt, subtask_1_25.txt, subtask_1_26.txt, subtask_1_27.txt, subtask_1_28.txt, subtask_1_29.txt, subtask_1_3.txt, subtask_1_30.txt, subtask_1_31.txt, subtask_1_32.txt, subtask_1_33.txt, subtask_1_34.txt, subtask_1_35.txt, subtask_1_36.txt, subtask_1_37.txt, subtask_1_38.txt, subtask_1_39.txt, subtask_1_4.txt, subtask_1_40.txt, subtask_1_41.txt, subtask_1_42.txt, subtask_1_43.txt, subtask_1_44.txt, subtask_1_45.txt, subtask_1_46.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt
Case Name Status Exec Time Memory
sample_01.txt AC 23 ms 4796 KB
sample_02.txt AC 16 ms 4692 KB
sample_03.txt AC 16 ms 4744 KB
subtask_1_1.txt AC 14 ms 4836 KB
subtask_1_10.txt AC 66 ms 6768 KB
subtask_1_11.txt AC 237 ms 8936 KB
subtask_1_12.txt AC 24 ms 4856 KB
subtask_1_13.txt AC 23 ms 4896 KB
subtask_1_14.txt AC 16 ms 4860 KB
subtask_1_15.txt AC 94 ms 5548 KB
subtask_1_16.txt AC 29 ms 4992 KB
subtask_1_17.txt AC 37 ms 4964 KB
subtask_1_18.txt AC 16 ms 4908 KB
subtask_1_19.txt AC 33 ms 5100 KB
subtask_1_2.txt AC 15 ms 4848 KB
subtask_1_20.txt AC 15 ms 4828 KB
subtask_1_21.txt AC 33 ms 4884 KB
subtask_1_22.txt AC 15 ms 4784 KB
subtask_1_23.txt AC 217 ms 6820 KB
subtask_1_24.txt AC 214 ms 6656 KB
subtask_1_25.txt AC 217 ms 6848 KB
subtask_1_26.txt AC 226 ms 6964 KB
subtask_1_27.txt AC 502 ms 10180 KB
subtask_1_28.txt AC 496 ms 10156 KB
subtask_1_29.txt AC 215 ms 6876 KB
subtask_1_3.txt AC 18 ms 4768 KB
subtask_1_30.txt AC 219 ms 6712 KB
subtask_1_31.txt AC 216 ms 6832 KB
subtask_1_32.txt AC 228 ms 7068 KB
subtask_1_33.txt AC 493 ms 10228 KB
subtask_1_34.txt AC 491 ms 10320 KB
subtask_1_35.txt AC 216 ms 6708 KB
subtask_1_36.txt AC 219 ms 6820 KB
subtask_1_37.txt AC 215 ms 6788 KB
subtask_1_38.txt AC 232 ms 7136 KB
subtask_1_39.txt AC 614 ms 11196 KB
subtask_1_4.txt AC 33 ms 5092 KB
subtask_1_40.txt AC 618 ms 11236 KB
subtask_1_41.txt AC 245 ms 6712 KB
subtask_1_42.txt AC 235 ms 6880 KB
subtask_1_43.txt AC 374 ms 9068 KB
subtask_1_44.txt AC 437 ms 10120 KB
subtask_1_45.txt AC 194 ms 6716 KB
subtask_1_46.txt AC 214 ms 6780 KB
subtask_1_5.txt AC 77 ms 6432 KB
subtask_1_6.txt AC 16 ms 4772 KB
subtask_1_7.txt AC 22 ms 4760 KB
subtask_1_8.txt AC 16 ms 4752 KB
subtask_1_9.txt AC 55 ms 5144 KB