Binary Search
Time limit4sMemory limit1024 MB
Given an undirected graph with 0/1 labels on vertices, find the length of the shortest binary string that no walk can spell, or report infinity.
- Level
Hard8 of 10
- Topics
- Graph, Dynamic programming, BFS
- Solved
- No attempts yet
Problem
You are given an undirected graph with vertices and edges. Each vertex has a number written on it, which is either or .
A walk is a sequence of vertices in the graph such that any two consecutive vertices are connected by an edge. A binary sequence is walkable if there is a walk with .
In other words, a binary sequence is walkable if you can walk through the graph and write down the numbers of the visited vertices in order to get . Figure B.1 shows Sample Input 1, where every binary sequence of length at most is walkable.

Find the length of a shortest binary sequence that is not walkable.
Input
The first line has two integers and (, ), the number of vertices and the number of edges.
The second line has integers , where each is either 0 or 1.
Each of the next lines has two integers and (, ), meaning vertices and are connected by an edge. Every pair of vertices is connected by at most one edge.
Output
If every binary sequence is walkable, print "infinity". Otherwise, print the length of a shortest binary sequence that is not walkable.