Congested Networks

Time limit1sMemory limit128 MB

Summary
For each connected graph with at most 40 nodes, find the maximum number of edge-disjoint paths between any pair of nodes.
Level

Medium7 of 10

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

Problem

You are given a connected computer network with bidirectional communication. You want to pick two distinct nodes uu and vv so that the congestion between them — produced by a virus continuously sent back and forth — is as large as possible. The congestion level between uu and vv is defined as the maximum number of edge-disjoint paths (paths that share no edge) connecting the two nodes.

For example, in the network shown below there are three paths between node 0 and node 6 such that no edge is used by more than one of them. Two paths are allowed to pass through the same node (for instance node 7). No other pair of nodes yields a higher congestion level.

Input

The input is a sequence of connected computer networks. Each network has nn nodes labeled {0,1,…,n−1}\{0, 1, \ldots, n-1\} with n≤40n \le 40.

Each network is specified as follows. The first line contains a single non-negative integer nn, the number of nodes. It is followed by nn lines; the ii-th of these lines lists the neighbors (zero-indexed) of node i−1i-1, separated by spaces.

The last network is followed by a network with n=0n = 0, which must not be processed. There may be up to 2000 networks.

Output

For each input network, output on its own line a single integer: the maximum congestion level achievable by some pair of nodes in that network.

Examples1

  1. Example 1

    Input
    8
    4 5 7
    5 6
    6 7
    7
    0 7
    0 1
    1 2 7
    0 2 3 4 6
    4
    1 2
    0 2
    3 0 1
    2
    0
    
    Expected output
    3
    2