1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #pragma GCC optimize("O2,O3,Ofast") #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<algorithm> #include<bitset> #include<vector> #include<queue> #define LL long long #define PII pair<int, int> using namespace std; template <class T> inline void read(T &res) { res = 0; bool flag = 0; char c = getchar(); while('0' > c || c > '9'){ if(c == '-') flag = 1; c = getchar(); } while('0' <= c && c <= '9') res = (res << 3) + (res << 1) + (c ^ 48), c = getchar(); if(flag) res = -res; } template <class T, class ...ARC> inline void read(T &res, ARC &...com){ read(res), read(com...); }
inline void write(char c) { putchar(c); }
inline void write(const char *s){ while(*s) putchar(*s ++); } template <class T> inline void write(T res) { if(res < 0) res = -res, putchar('-'); if(res > 9) write(res / 10); putchar(res % 10 + '0'); } template <class T, class ...ARC> inline void write(T res, ARC ...com){ write(res), write(com...); } const int N = 2e5 + 5, M = 2.2e7 + 5, K = 1.5e6 + 5, Inf = 1e9; int n, m; int idx; int e[M], ne[M], h[K], w[M]; int v[K]; inline void add(int x, int y, int z) { idx ++; e[idx] = y, ne[idx] = h[x], h[x] = idx, w[idx] = z; } int a[N]; int flag[M], id[N], cnt; int q[K << 1], hh, tt; int main() { memset(h, -1, sizeof(h)); read(n, m); int maxn = 0; for(int i = 1;i <= n;i ++) read(a[i]), maxn = max(maxn, a[i]); for(int i = 1;i <= 20;i ++) if((1 << i) > maxn) { maxn = (1 << i) - 1; break; } cnt = maxn; for(int i = 1;i <= n;i ++) id[i] = ++ cnt, add(id[i], a[i], 1), add(a[i], id[i], 0); for(int i = 1, o, u;i <= m;i ++) read(o, u), add(id[o], id[u], 1); for(int i = maxn;i >= 0;i --) { for(int j = 0;j < 20;j ++) if(i >> j & 1) add(i, i ^ (1 << j), 0); } memset(v, 0x3f, sizeof(v)); hh = tt = cnt; hh ++; q[++ tt] = id[1]; v[id[1]] = 0; while(hh <= tt) { int o = q[hh ++]; if(flag[o]) continue; flag[o] = 1; for(int i = h[o], j; ~i;i = ne[i]) { j = e[i]; if(v[j] > v[o] + w[i]) { v[j] = v[o] + w[i]; if(w[i]) q[++ tt] = j; else q[-- hh] = j; } } } for(int i = 1;i <= n;i ++) { if(v[id[i]] > n) write(-1, '\n'); else write(v[id[i]], '\n'); } return 0; }
|