ソースコード 拡げる

Copy
```#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#include<bitset>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
#include<complex>
#include<cassert>
#include<random>
using namespace std;
#include<atcoder/all>
using namespace atcoder;

#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)

typedef long long ll;
const ll INF = 1e17;
const ll MAX = 4000001;
const long double eps = 1E-14;

ll max(ll a, ll b) {
if (a > b) { return a; }
return b;
}

ll min(ll a, ll b) {
if (a > b) { return b; }
return a;
}

ll gcd(ll a, ll b) {
if (b == 0) { return a; }
if (a < b) { return gcd(b, a); }
return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}

struct edge {
ll ind;
ll fr;
ll to;
ll d;
};

using mint = modint;

typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;

typedef vector<mint> vmint;
typedef vector<vector<mint>> vvmint;
typedef vector<vector<vector<mint>>> vvvmint;

vmint f, finv, inv;

void cominit(ll N) {
ll MOD = modint::mod();//デフォルトが998244353に注意

f.assign(N + 1, 1);
finv.assign(N + 1, 1);
inv.assign(N + 1, 1);
inv[1] = 1;

repn(i, N) {
f[i] = f[i - 1] * i;
if (i > 1)inv[i] = -inv[MOD % i] * (MOD / i);
finv[i] = finv[i - 1] * inv[i];
}
}

mint com(ll a, ll b) {
if (a < 0 || b < 0 || a < b) { return 0; }
return f[a] * finv[b] * finv[a - b];
}

/////////////////////////////////////

int main() {
ll N, M;
cin >> N >> M;
dsu d(N + 1);

vll par(N + M + 1);
vll rt(N + 1);
repn(i, N + M)par[i] = i;
repn(i, N)rt[i] = i;

repn(i, M) {
ll a, b;
cin >> a >> b;
if (a == b) { continue; }
par[rt[a]] = N + i;
par[rt[b]] = N + i;

d.merge(a, b);
}

vll ht(N + M + 1,0);
for (ll i = N + M; i >= 1; i--) {
ht[i] = ht[par[i]] + 1;
}

vvll dob(20, vll(N + M + 1, 0));

repn(i, N + M)dob[0][i] = par[i];
repn(j, 19)repn(i, N + M) {
dob[j][i] = dob[j - 1][dob[j - 1][i]];
}

ll Q;
cin >> Q;
repn(q, Q) {
ll x, y;
cin >> x >> y;
if (ht[x] < ht[y]) { swap(x, y); }

ll dif = ht[x] - ht[y];
rep(k, 20) {
if ((dif >> k) & 1) { x = dob[k][x]; }
}

if (x == y) { cout << x - N << endl; continue; }
for (ll k = 19; k >= 0; k--) {
if (dob[k][x] != dob[k][y]) {
x = dob[k][x];
y = dob[k][y];
}
}

if (par[x] != par[y]) { cout << -1 << endl; }
else { cout << par[x]-N << endl; }

}

}
```

#### 提出情報

提出日時 2020-11-18 11:49:29+0900 H - Union Sets dokin C++ (GCC 9.2.1) 600 2736 Byte AC 382 ms 40508 KB

#### ジャッジ結果

セット名 Sample All

 AC × 3
 AC × 49
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
ケース名 結果 実行時間 メモリ
sample_01.txt AC 9 ms 3584 KB
sample_02.txt AC 2 ms 3624 KB
sample_03.txt AC 2 ms 3576 KB
subtask_1_1.txt AC 2 ms 3500 KB
subtask_1_10.txt AC 38 ms 13696 KB
subtask_1_11.txt AC 158 ms 19708 KB
subtask_1_12.txt AC 17 ms 12744 KB
subtask_1_13.txt AC 8 ms 4296 KB
subtask_1_14.txt AC 3 ms 3636 KB
subtask_1_15.txt AC 63 ms 3744 KB
subtask_1_16.txt AC 12 ms 4344 KB
subtask_1_17.txt AC 26 ms 3784 KB
subtask_1_18.txt AC 3 ms 3544 KB
subtask_1_19.txt AC 25 ms 12024 KB
subtask_1_2.txt AC 2 ms 3596 KB
subtask_1_20.txt AC 4 ms 3752 KB
subtask_1_21.txt AC 24 ms 3616 KB
subtask_1_22.txt AC 13 ms 8316 KB
subtask_1_23.txt AC 363 ms 22456 KB
subtask_1_24.txt AC 366 ms 22452 KB
subtask_1_25.txt AC 382 ms 22372 KB
subtask_1_26.txt AC 369 ms 23980 KB
subtask_1_27.txt AC 309 ms 40464 KB
subtask_1_28.txt AC 319 ms 40276 KB
subtask_1_29.txt AC 371 ms 22456 KB
subtask_1_3.txt AC 4 ms 3588 KB
subtask_1_30.txt AC 351 ms 22456 KB
subtask_1_31.txt AC 345 ms 22320 KB
subtask_1_32.txt AC 355 ms 23932 KB
subtask_1_33.txt AC 309 ms 40428 KB
subtask_1_34.txt AC 307 ms 40448 KB
subtask_1_35.txt AC 341 ms 22472 KB
subtask_1_36.txt AC 347 ms 22320 KB
subtask_1_37.txt AC 345 ms 22452 KB
subtask_1_38.txt AC 344 ms 23988 KB
subtask_1_39.txt AC 349 ms 40508 KB
subtask_1_4.txt AC 29 ms 18700 KB
subtask_1_40.txt AC 353 ms 40412 KB
subtask_1_41.txt AC 167 ms 3596 KB
subtask_1_42.txt AC 170 ms 3588 KB
subtask_1_43.txt AC 256 ms 27752 KB
subtask_1_44.txt AC 287 ms 40280 KB
subtask_1_45.txt AC 169 ms 3652 KB
subtask_1_46.txt AC 376 ms 22556 KB
subtask_1_5.txt AC 38 ms 12920 KB
subtask_1_6.txt AC 17 ms 13136 KB
subtask_1_7.txt AC 5 ms 4156 KB
subtask_1_8.txt AC 6 ms 5564 KB
subtask_1_9.txt AC 49 ms 5052 KB