Binary Search

시간 제한4초메모리 제한1024 MB

요약
각 정점에 0 또는 1이 적힌 무방향 그래프에서 어떤 보행으로도 만들 수 없는 가장 짧은 이진 문자열의 길이를 구하고, 모든 문자열이 가능하면 infinity를 출력한다.
난이도

보통10점 중 6점

유형
그래프, BFS, 문자열, 조합론
정답자
아직 제출이 없습니다

문제

You are given an undirected graph with nn vertices and mm edges. Each vertex vv has a number a_va\_v written on it. This number is either 00 or 11. A walk is a sequence v_1v_2…v_kv\_1v\_2 \dots v\_k of vertices in the graph such that any two consecutive vertices are connected by an edge. We call a binary sequence s=s_1s_2…s_ks = s\_1s\_2 \dots s\_k walkable if there is a walk v_1v_2…v_kv\_1v\_2 \dots v\_k in the graph that satisfies a_v_1a_v_2…a_v_k=sa\_{v\_1} a\_{v\_2} \dots a\_{v\_k} = s.

In other words, a binary sequence is walkable if it is possible to obtain ss by walking in the graph and writing down the binary numbers in the order that they are visited. An example is visualized in Figure B.1.

Figure B.1: Illustration of Sample Input 1. Every binary sequence of length at most 33 is walkable.

Your task is to find the length of a shortest binary sequence that is not walkable.

입력

The input consists of:

  • One line with two integers nn and mm (1≤n≤3⋅1051 \leq n \leq 3 \cdot 10^5, 0≤m≤3⋅1050 \leq m \leq 3 \cdot 10^5), the number of vertices and the number of edges.
  • One line with nn integers a_1,…,a_na\_1,\dots, a\_n (a_v∈0,1a\_v \in \\{0, 1\\} for each vv), where a_va\_v is the number written on vertex vv.
  • mm lines, each with two integers uu and vv (1≤u,v≤n1 \leq u,v \leq n, u≠vu \neq v), denoting that the vertices uu and vv are connected by an edge. It is guaranteed that every pair of vertices is connected by at most one edge.

출력

If every binary sequence is walkable, output "infinity". Otherwise, output the length of a shortest binary sequence that is not walkable.

예제3

  1. 예제 1

    입력
    4 4
    0 0 1 1
    1 2
    1 3
    2 3
    3 4
    
    예상 출력
    4
    
  2. 예제 2

    입력
    6 7
    0 0 1 1 0 1
    1 2
    3 1
    1 4
    2 3
    4 2
    3 4
    5 6
    
    예상 출력
    infinity
    
  3. 예제 3

    입력
    1 0
    0
    
    예상 출력
    1