Submission #19238672


Source Code Expand

Copy
#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

int main(){
    int N, M, Q;
    cin >> N >> M;
    int a[M], b[M];
    rep(i, M){
        cin >> a[i] >> b[i];
        a[i]--; b[i]--;
    }
    cin >> Q;
    int x[Q], y[Q], ok[Q], ng[Q];
    rep(i, Q){
        cin >> x[i] >> y[i];
        x[i]--; y[i]--;
        ok[i] = M;
        ng[i] = -1;
    }
    while(true){
        bool fin = true;
        vector<vector<int>> mid_idx(M);
        rep(i, Q){
            if(ok[i] - ng[i] > 1){
                fin = false;
                int mid = (ok[i] + ng[i]) / 2;
                mid_idx[mid].emplace_back(i);
            }
        }
        if(fin) break;
        dsu d(N);
        rep(i, M){
            d.merge(a[i], b[i]);
            for(int idx : mid_idx[i]){
                if(d.same(x[idx], y[idx])) ok[idx] = i;
                else ng[idx] = i;
            }
        }
    }
    rep(i, Q){
        if(ok[i] < M) cout << ok[i] + 1 << endl;
        else cout << -1 << endl;
    }
    return 0;
}

Submission Info

Submission Time
Task H - Union Sets
User math_hiyoko
Language C++ (GCC 9.2.1)
Score 600
Code Size 1147 Byte
Status AC
Exec Time 293 ms
Memory 9816 KB

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 9 ms 3608 KB
sample_02.txt AC 2 ms 3564 KB
sample_03.txt AC 2 ms 3508 KB
subtask_1_1.txt AC 3 ms 3500 KB
subtask_1_10.txt AC 39 ms 5272 KB
subtask_1_11.txt AC 164 ms 7752 KB
subtask_1_12.txt AC 10 ms 3824 KB
subtask_1_13.txt AC 10 ms 3812 KB
subtask_1_14.txt AC 3 ms 3652 KB
subtask_1_15.txt AC 70 ms 4216 KB
subtask_1_16.txt AC 10 ms 3752 KB
subtask_1_17.txt AC 26 ms 3640 KB
subtask_1_18.txt AC 6 ms 3532 KB
subtask_1_19.txt AC 14 ms 3860 KB
subtask_1_2.txt AC 2 ms 3512 KB
subtask_1_20.txt AC 5 ms 3668 KB
subtask_1_21.txt AC 15 ms 3668 KB
subtask_1_22.txt AC 4 ms 3608 KB
subtask_1_23.txt AC 185 ms 5540 KB
subtask_1_24.txt AC 188 ms 5584 KB
subtask_1_25.txt AC 190 ms 5508 KB
subtask_1_26.txt AC 203 ms 5756 KB
subtask_1_27.txt AC 292 ms 9744 KB
subtask_1_28.txt AC 293 ms 9672 KB
subtask_1_29.txt AC 187 ms 5460 KB
subtask_1_3.txt AC 7 ms 3516 KB
subtask_1_30.txt AC 189 ms 5548 KB
subtask_1_31.txt AC 191 ms 5508 KB
subtask_1_32.txt AC 203 ms 5844 KB
subtask_1_33.txt AC 289 ms 9560 KB
subtask_1_34.txt AC 291 ms 9620 KB
subtask_1_35.txt AC 183 ms 5476 KB
subtask_1_36.txt AC 187 ms 5512 KB
subtask_1_37.txt AC 193 ms 5496 KB
subtask_1_38.txt AC 200 ms 5888 KB
subtask_1_39.txt AC 282 ms 9788 KB
subtask_1_4.txt AC 19 ms 3932 KB
subtask_1_40.txt AC 284 ms 9816 KB
subtask_1_41.txt AC 162 ms 5256 KB
subtask_1_42.txt AC 175 ms 5496 KB
subtask_1_43.txt AC 245 ms 8152 KB
subtask_1_44.txt AC 274 ms 9680 KB
subtask_1_45.txt AC 161 ms 5076 KB
subtask_1_46.txt AC 185 ms 4996 KB
subtask_1_5.txt AC 41 ms 5304 KB
subtask_1_6.txt AC 8 ms 3832 KB
subtask_1_7.txt AC 4 ms 3588 KB
subtask_1_8.txt AC 3 ms 3636 KB
subtask_1_9.txt AC 43 ms 3980 KB