## Editorial for WC '15 Contest 2 S3 - Revenge of the Sith

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

We are given an undirected graph with nodes and edges. If Anakin and Obi-Wan are placed at two random nodes on the graph (call them nodes and ), we would like to know the probability that they will eventually reach the same node by each traveling one edge per second (if they can). First we should consider what determines whether or not a pair of starting locations and will yield an eventual encounter between them.

If and are not reachable from one another at all (in other words, if the two nodes are in different connected components of the graph), then clearly they won't ever encounter each other.

Otherwise if they are part of the same connected component, they most likely will encounter each other. However, it actually depends on the layout of the connected component. To figure out which type of components they will never encounter each other, we can try drawing ourselves some examples. Here are some you might come up with. Say starts on a blue node, and starts on an orange node. Each person will never end up on a chamber of the opposite colour than the colour he started on. Therefore, they will never meet if they start on opposite colours.

Note that in all of these examples, there is never an edge connecting two nodes of the same colour. This is because if such an edge were to exist, then the "parity" of their opposing movements would be broken, and it would be possible to eventually maneuver them onto the same node. In fact, this type of graph is well-known as "bipartite." A component is bipartite if and only if there exists a way to label each of its nodes with one of two colours such that no edge connects two nodes with the same colour.

If the component is bipartite and and both start on different colours, then this pair of starting locations will never result in a duel, since Obi-Wan and Anakin's nodes will be guaranteed to have different labels at every point in time (they'll simultaneously alternate between blue and orange every minute). If and would have the same label as one another or if their component is not bipartite, then an encounter can occur (which means that it certainly will within an infinite amount of time). Convince yourself that bipartite components are actually the only type of components in which a duel can never occur.

The probability of a random pair of starting positions eventually leading to a duel is equal to the total number of starting pairs that result in a duel, divided by the total number of possible starting pairs (equal to , since can each take on the nodes from to ). We can do this by trying to classify each component of the graph as either bipartite or not. Breadth-first search (BFS) or depth-first search (DFS) are popular ways to do so.

Consider each connected component of the graph in turn – we will start BFS or DFSing from any node within the component, meaning trying to greedily label the component's nodes with alternating colours (say blue is and orange is ), until they've all been labelled or a conflict has been found (with a node adjacent to nodes with multiple different labels). In the latter case, the component is not bipartite, meaning that it contains valid pairs of starting locations (where is the number of nodes in it). Otherwise, it contains valid pairs (where and are the number of nodes labelled with and , respectively). Traversing the graph to find the connected components and attempting to label their nodes can be done in time, since a node or edge never needs to be examined more than once. A program can implement this solution exactly using an adjacency list, which is an efficient way to store and traverse graphs. More readings on bipartite graphs can be found online with a quick search.