// Similar to the powerset construction, but only applied to substrings of // the cyclic string repeating 0011. This constructs linearly many sets of // linear size, for quadratic overall running time. // // Fails on certain cases with isolated nodes. // @EXPECTED_RESULTS@: WRONG_ANSWER, TIME_LIMIT_EXCEEDED #include #include #include #include #include #include #include using namespace std; using i64 = long long; const int N = 3e5 + 5; const i64 MOD = i64(1e18) + 3; i64 p2[N]; int X = 0; int mark[N]; int main() { p2[0] = 1; for (int n = 1; n < N; n++) { p2[n] = 2*p2[n-1] % MOD; } int n, m; cin >> n >> m; vector a(n); for (int &x: a) cin >> x; vector> adj(n); while (m--) { int u, v; cin >> u >> v; u--, v--; adj[u].push_back(v); adj[v].push_back(u); } int res = 1e9; for (int k = 0; k < 4; k++) { vector cur(n); iota(begin(cur), end(cur), 0); unordered_set seen; for (int i = 0; ; i++) { if (cur.empty()) { res = min(res, i); break; } i64 hash = 0; for (int x: cur) hash = (hash + p2[x]) % MOD; if (seen.count(hash)) break; seen.insert(hash); vector nxt; X++; for (int x: cur) for (int y: adj[x]) { if (a[y] == ((i+k) % 4) / 2) continue; if (mark[y] != X) { mark[y] = X; nxt.push_back(y); } } cur = nxt; } } if (res < 1e9) cout << res << '\n'; else cout << "infinity" << '\n'; }