Extra problem X scorpion

The problem is to rapidly determine if a given graph is a `scorpion'

A scorpion is an undirected graph on n vertices that has a vertex of degree 1 (the tail), connected to a vertex of degree 2 (the body), whose second connection is to a vertex (the head) of degree n-2. Thus all the other vertices (the legs) are connected to the head, but not to the body or tail. For any two legs there may or may not be an edge connecting them. The code in 621/scorpion.cc contains a predicate isScorpion() which determines in O(n) time whether a graph on n vertices is a scorpion. The predicate works on a graph given by the vector of adjacency lists representation.

The problem assigned here is to write an `isScorpion' predicate that works on a graph given by the adjacency matrix representation. Thus the graph is given by a n by n matrix A with A[i,j] == 1 iff (i,j) is an edge of the graph.

Of course the predicate will have to be written differently than the one for the vector of adjacency lists representation. This would not be so hard, but change of representation is not all: The requirement is that the predicate work in O(n) time, despite the matrix representation! For instance, you cannot scan the whole matrix even once, which would take O(n2) time!


Reminder: As with regular homework, this is to be done with your own brain cells. It is not a literature research or consulting exercise.