Under construction

Still being built. Things may change

Connecting Supertrees

Time limit1sMemory limit1024 MB

Summary
Given an n by n matrix of path counts (each 0 to 3), build a simple undirected graph of bridges that matches every p[i][j], or report that none exists.
Level

Hard8 of 10

Topics
Graph, Union-find, Implementation
Solved
No attempts yet

Problem

Gardens by the Bay is a large nature park in Singapore. The park has nn towers, known as supertrees, labelled 0 to n−1n-1. Build a set of zero or more bridges. Each bridge connects a pair of distinct towers and can be traversed in either direction. No two bridges may connect the same pair of towers.

A path from tower xx to tower yy is a sequence of one or more towers such that:

  • the first element is xx,
  • the last element is yy,
  • all elements are distinct, and
  • each two consecutive elements are connected by a bridge.

By definition, there is exactly one path from a tower to itself. The number of paths from tower ii to tower jj is the same as the number of paths from tower jj to tower ii.

The architect wants the bridges built so that for all 0≤i,j≤n−10 \le i, j \le n-1 there are exactly p[i][j]p[i][j] different paths from tower ii to tower jj, where 0≤p[i][j]≤30 \le p[i][j] \le 3. Construct a set of bridges that meets these requirements, or determine that it is impossible.

Constraints

  • 1≤n≤10001 \le n \le 1000
  • p[i][i]=1p[i][i] = 1 for all 0≤i≤n−10 \le i \le n-1
  • p[i][j]=p[j][i]p[i][j] = p[j][i] for all 0≤i,j≤n−10 \le i, j \le n-1
  • 0≤p[i][j]≤30 \le p[i][j] \le 3 for all 0≤i,j≤n−10 \le i, j \le n-1

Examples1

  1. Example 1

    Input
    1
    1
    
    Expected output
    1
    0