ソースコード 拡げる

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);

for (int dum = 0; dum < 20; dum++) {
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;

}
```

#### 提出情報

提出日時 2021-01-22 00:54:14+0900 H - Union Sets parsley69 C++ (GCC 9.2.1) 600 2051 Byte AC 277 ms 10280 KB

#### コンパイルエラー

```./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[]) {
|                    ~~~~~~~~~~~~~^~~~~~
```

#### ジャッジ結果

セット名 Sample All

 AC × 3
 AC × 49
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
ケース名 結果 実行時間 メモリ
sample_01.txt AC 5 ms 3504 KB
sample_02.txt AC 3 ms 3516 KB
sample_03.txt AC 2 ms 3508 KB
subtask_1_1.txt AC 2 ms 3620 KB
subtask_1_10.txt AC 30 ms 5040 KB
subtask_1_11.txt AC 138 ms 7448 KB
subtask_1_12.txt AC 15 ms 3664 KB
subtask_1_13.txt AC 11 ms 3856 KB
subtask_1_14.txt AC 3 ms 3628 KB
subtask_1_15.txt AC 60 ms 4380 KB
subtask_1_16.txt AC 13 ms 3840 KB
subtask_1_17.txt AC 27 ms 3680 KB
subtask_1_18.txt AC 3 ms 3652 KB
subtask_1_19.txt AC 12 ms 3964 KB
subtask_1_2.txt AC 2 ms 3592 KB
subtask_1_20.txt AC 3 ms 3604 KB
subtask_1_21.txt AC 15 ms 3752 KB
subtask_1_22.txt AC 4 ms 3708 KB
subtask_1_23.txt AC 174 ms 6120 KB
subtask_1_24.txt AC 174 ms 6100 KB
subtask_1_25.txt AC 173 ms 6112 KB
subtask_1_26.txt AC 187 ms 6276 KB
subtask_1_27.txt AC 261 ms 9624 KB
subtask_1_28.txt AC 263 ms 9812 KB
subtask_1_29.txt AC 172 ms 6016 KB
subtask_1_3.txt AC 3 ms 3660 KB
subtask_1_30.txt AC 173 ms 5972 KB
subtask_1_31.txt AC 176 ms 6072 KB
subtask_1_32.txt AC 184 ms 6300 KB
subtask_1_33.txt AC 260 ms 9748 KB
subtask_1_34.txt AC 259 ms 9616 KB
subtask_1_35.txt AC 174 ms 5988 KB
subtask_1_36.txt AC 174 ms 5968 KB
subtask_1_37.txt AC 176 ms 6152 KB
subtask_1_38.txt AC 186 ms 6424 KB
subtask_1_39.txt AC 275 ms 10280 KB
subtask_1_4.txt AC 19 ms 3980 KB
subtask_1_40.txt AC 277 ms 10204 KB
subtask_1_41.txt AC 161 ms 5420 KB
subtask_1_42.txt AC 174 ms 5420 KB
subtask_1_43.txt AC 216 ms 8044 KB
subtask_1_44.txt AC 236 ms 9620 KB
subtask_1_45.txt AC 149 ms 3888 KB
subtask_1_46.txt AC 152 ms 3996 KB
subtask_1_5.txt AC 37 ms 5032 KB
subtask_1_6.txt AC 13 ms 3704 KB
subtask_1_7.txt AC 3 ms 3668 KB
subtask_1_8.txt AC 3 ms 3604 KB
subtask_1_9.txt AC 42 ms 4044 KB