Submission #19555250


Source Code Expand

Copy
#include <iostream>
#include <iomanip>
#include <fstream>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <chrono>

using namespace std;

struct UnionFind {
    vector<int> par;
    vector<int> rank;
    UnionFind(int N) : par(N), rank(N) {
        for(int i = 0; i < N; i++) {
            par[i] = i;
            rank[i] = 0;
        }
    }
    
    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }
    
    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) rank[x]++;
        par[y] = x;
        return true;
    }
    
    bool same(int x, int y) {
        return root(x) == root(y);
    }
};

int main(int argc, const char * argv[]) {

    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int N, M; cin >> N >> M;
    vector<int> a(M), b(M);
    for (int i = 0; i < M; i++) {
        cin >> a[i] >> b[i];
        a[i]--; b[i]--;
    }
    int Q; cin >> Q;
    vector<int> x(Q), y(Q);
    for (int i = 0; i < Q; i++) {
        cin >> x[i] >> y[i];
        x[i]--; y[i]--;
    }
    
    if (M == 0) {
        for (int i = 0; i < Q; i++) cout << -1 << endl;
        return 0;
    }
    
    vector<int> yes(Q, M + 1), no(Q, 0);
    
    while (true) {
        bool update = false;
        for (int i = 0; i < Q; i++) {
            if (yes[i] - no[i] != 1) {
                update = true;
                break;
            }
        }
        if (!update) break;
        
        vector<vector<int>> mid(M + 1);
        for (int i = 0; i < Q; i++) mid[(yes[i] + no[i]) / 2].emplace_back(i);
        
        UnionFind tree(N);
        
        for (int i = 1; i <= M; i++) {
            tree.unite(a[i - 1], b[i - 1]);
            for (int j : mid[i]) {
                if (tree.same(x[j], y[j])) yes[j] = i; else no[j] = i;
            }
        }
    }
    
    for (int i = 0; i < Q; i++) {
        cout << (yes[i] == M + 1 ? -1 : yes[i]) << endl;
    }
    
    return 0;
    
}

Submission Info

Submission Time
Task H - Union Sets
User parsley69
Language C++ (GCC 9.2.1)
Score 600
Code Size 2259 Byte
Status AC
Exec Time 248 ms
Memory 9976 KB

Compile Error

./Main.cpp: In function ‘int main(int, const char**)’:
./Main.cpp:45:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
   45 | int main(int argc, const char * argv[]) {
      |          ~~~~^~~~
./Main.cpp:45:33: warning: unused parameter ‘argv’ [-Wunused-parameter]
   45 | int main(int argc, const char * argv[]) {
      |                    ~~~~~~~~~~~~~^~~~~~

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 11 ms 3588 KB
sample_02.txt AC 2 ms 3648 KB
sample_03.txt AC 2 ms 3560 KB
subtask_1_1.txt AC 4 ms 3620 KB
subtask_1_10.txt AC 36 ms 5112 KB
subtask_1_11.txt AC 129 ms 7472 KB
subtask_1_12.txt AC 7 ms 3740 KB
subtask_1_13.txt AC 9 ms 3788 KB
subtask_1_14.txt AC 3 ms 3472 KB
subtask_1_15.txt AC 63 ms 4236 KB
subtask_1_16.txt AC 9 ms 3776 KB
subtask_1_17.txt AC 19 ms 3800 KB
subtask_1_18.txt AC 3 ms 3588 KB
subtask_1_19.txt AC 10 ms 3896 KB
subtask_1_2.txt AC 3 ms 3592 KB
subtask_1_20.txt AC 4 ms 3652 KB
subtask_1_21.txt AC 18 ms 3756 KB
subtask_1_22.txt AC 5 ms 3720 KB
subtask_1_23.txt AC 162 ms 6024 KB
subtask_1_24.txt AC 159 ms 6060 KB
subtask_1_25.txt AC 167 ms 5968 KB
subtask_1_26.txt AC 178 ms 6424 KB
subtask_1_27.txt AC 248 ms 9684 KB
subtask_1_28.txt AC 246 ms 9636 KB
subtask_1_29.txt AC 157 ms 6128 KB
subtask_1_3.txt AC 3 ms 3512 KB
subtask_1_30.txt AC 163 ms 6080 KB
subtask_1_31.txt AC 164 ms 6152 KB
subtask_1_32.txt AC 177 ms 6468 KB
subtask_1_33.txt AC 242 ms 9776 KB
subtask_1_34.txt AC 247 ms 9812 KB
subtask_1_35.txt AC 157 ms 5940 KB
subtask_1_36.txt AC 161 ms 6076 KB
subtask_1_37.txt AC 163 ms 6108 KB
subtask_1_38.txt AC 177 ms 6364 KB
subtask_1_39.txt AC 247 ms 9976 KB
subtask_1_4.txt AC 15 ms 3936 KB
subtask_1_40.txt AC 247 ms 9968 KB
subtask_1_41.txt AC 148 ms 5396 KB
subtask_1_42.txt AC 156 ms 5548 KB
subtask_1_43.txt AC 201 ms 8100 KB
subtask_1_44.txt AC 224 ms 9580 KB
subtask_1_45.txt AC 150 ms 3880 KB
subtask_1_46.txt AC 153 ms 3824 KB
subtask_1_5.txt AC 32 ms 4988 KB
subtask_1_6.txt AC 12 ms 3784 KB
subtask_1_7.txt AC 2 ms 3556 KB
subtask_1_8.txt AC 2 ms 3684 KB
subtask_1_9.txt AC 35 ms 4040 KB