Similar Items: Guarded Second-Order Logic, Spanning Trees, and Network Flows