123502024-12-12 20:45:43szilAzugandcpp17Accepted 100/100531ms1752 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MAXN = 200'001;
const int K = 21;
const ll INF = 1e9;

int v[MAXN], dist[K][K];

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int n, q; cin >> n >> q;
	for (int i = 1; i <= n; i++) {
		cin >> v[i];
	}
	for (int i = 0; i < K; i++) {
		for (int j = 0; j < K; j++) {
			if (i != j) dist[i][j] = INF;
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < K; j++) {
			for (int k = 0; k < K; k++) {
				if ((v[i] & (1<<j)) && (v[i] & (1<<k))) {
					dist[j][k] = min(dist[j][k], 1);
				}
			}
		}
	}
	for (int k = 0; k < K; k++) {
		for (int i = 0; i < K; i++) {
			for (int j = 0; j < K; j++) {
				dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
			}
		}
	}
	while (q--) {
		int a, b; cin >> a >> b;
		if (a == b) {
			cout << "0\n";
			continue;
		}
		int ans = INF;
		for (int i = 0; i < K; i++) {
			for (int j = 0; j < K; j++) {
				if ((v[a] & (1<<i)) && (v[b] & (1<<j))) {
					ans = min(ans, dist[i][j]);
				}
			}
		}
		cout << (ans==INF?-1:ans+1) << "\n";
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms320 KiB
2Accepted1ms320 KiB
subtask27/7
3Accepted2ms320 KiB
4Accepted2ms320 KiB
5Accepted2ms320 KiB
6Accepted2ms320 KiB
7Accepted2ms320 KiB
8Accepted2ms320 KiB
9Accepted2ms320 KiB
10Accepted2ms320 KiB
11Accepted1ms320 KiB
12Accepted2ms320 KiB
13Accepted2ms320 KiB
subtask323/23
14Accepted179ms1080 KiB
15Accepted182ms980 KiB
16Accepted182ms1080 KiB
17Accepted217ms1128 KiB
18Accepted234ms1212 KiB
19Accepted241ms1208 KiB
20Accepted159ms1080 KiB
21Accepted160ms1212 KiB
22Accepted160ms1080 KiB
23Accepted160ms1080 KiB
24Accepted209ms1080 KiB
25Accepted209ms1112 KiB
subtask421/21
26Accepted303ms1592 KiB
27Accepted293ms1592 KiB
28Accepted300ms1444 KiB
29Accepted310ms1592 KiB
30Accepted312ms1596 KiB
31Accepted307ms1596 KiB
32Accepted291ms1592 KiB
33Accepted296ms1472 KiB
34Accepted294ms1592 KiB
35Accepted293ms1592 KiB
subtask549/49
36Accepted1ms512 KiB
37Accepted1ms320 KiB
38Accepted2ms320 KiB
39Accepted2ms320 KiB
40Accepted2ms320 KiB
41Accepted2ms320 KiB
42Accepted2ms320 KiB
43Accepted2ms320 KiB
44Accepted2ms320 KiB
45Accepted2ms320 KiB
46Accepted1ms320 KiB
47Accepted2ms320 KiB
48Accepted2ms320 KiB
49Accepted179ms1080 KiB
50Accepted182ms980 KiB
51Accepted182ms1080 KiB
52Accepted217ms1128 KiB
53Accepted234ms1212 KiB
54Accepted241ms1208 KiB
55Accepted159ms1080 KiB
56Accepted160ms1212 KiB
57Accepted160ms1080 KiB
58Accepted160ms1080 KiB
59Accepted209ms1080 KiB
60Accepted209ms1112 KiB
61Accepted303ms1592 KiB
62Accepted293ms1592 KiB
63Accepted300ms1444 KiB
64Accepted310ms1592 KiB
65Accepted312ms1596 KiB
66Accepted307ms1596 KiB
67Accepted291ms1592 KiB
68Accepted296ms1472 KiB
69Accepted294ms1592 KiB
70Accepted293ms1592 KiB
71Accepted345ms1592 KiB
72Accepted368ms1500 KiB
73Accepted349ms1612 KiB
74Accepted531ms1592 KiB
75Accepted486ms1592 KiB
76Accepted514ms1464 KiB
77Accepted509ms1464 KiB
78Accepted308ms1592 KiB
79Accepted310ms1592 KiB
80Accepted307ms1600 KiB
81Accepted321ms1752 KiB
82Accepted449ms1592 KiB
83Accepted469ms1592 KiB
84Accepted462ms1488 KiB