UV 140 Bandwidth (DFS backtracking pruning) and bandwidthdfs

Source: Internet
Author: User

UV 140 Bandwidth (DFS backtracking pruning) and bandwidthdfs

An undirected graph is an arrangement of all its vertices. The maximum subscript difference between a vertex and all other vertices having edges is the bandwidth of This vertex and the maximum bandwidth of all vertices. find the minimum bandwidth for this graph

Enumerative arrange ans record the minimum bandwidth currently found in the enumeration process. Once the bandwidth is greater than ans, you do not need to extend it. After enumeration, you will get the answer.

# Include <cstdio> # include <cstring> using namespace std; const int N = 50; int n, ans, h [N], vis [N], a [N], d [N] [N], p [N]; void dfs (int cur, int band) {if (cur> = n & band <ans) {ans = band; for (int I = 0; I <cur; ++ I) p [I] = a [I]; // The Optimal Path recorded by the p array} for (int I = 0; cur <n & I <26; ++ I) {if (vis [I] |! H [I]) continue; a [cur] = I, vis [I] = 1; for (int j = 0; j <cur; ++ j) if (d [I] [a [j] & band <cur-j) band = cur-j; if (band> ans) {vis [I] = 0; return;} dfs (cur + 1, band); vis [I] = 0 ;}} int main () {char s [N]; while (scanf ("% s", s), strcmp (s, "#") {memset (d, 0, sizeof (d); memset (h, 0, sizeof (h); // h Array records whether the letter 'A' + I appears in the graph. n is the number of vertices. int I = n = 0, u, v; while (s [I]) {u = s [I]-'A'; if (! H [u]) h [u] = ++ n; I + = 2; while (s [I] & s [I]! = ';') {V = s [I ++]-'A'; if (! H [v]) h [v] = ++ n; d [u] [v] = d [v] [u] = 1;} if (! S [I ++]) break;} ans = N; memset (vis, 0, sizeof (vis); dfs (0, 0); for (int I = 0; I <n; ++ I) printf ("% c", 'A' + p [I]); printf ("-> % d \ n", ans );} return 0 ;}

Bandwidth

Given a graph (V, E) where V is a set of nodes and E is a set of arcs in VxV, andOrderingOn the elements in V, thenBandwidthOf a nodeVIs defined as the maximum distance in the orderingVAnd any node to which it is connected in the graph. The bandwidth of the ordering is then defined as the maximum of the individual bandwidths. For example, consider the following graph:

This can be ordered in several ways, two of which are using strated below:

For these orderings, the bandwidths of the nodes (in order) are 6, 6, 1, 4, 1, 1, 6, 6 giving an ordering bandwidth of 6, and 5, 3, 1, 4, 3, 5, 1, 4 giving an ordering bandwidth of 5.

Write a program that will find the ordering of a graph that minimises the bandwidth.

Input

Input will consist of a series of graphs. Each graph will appear on a line by itself. The entire file will be terminated by a line consisting of a single#. For each graph, the input will consist of a series of records separated ';'. each record will consist of a node name (a single upper case character in the range 'A' to 'Z'), followed by ': and at least one of its neighbors. the graph will contain no more than 8 nodes.

Output

Output will consist of one line for each graph, listing the ordering of the nodes followed by an arrow (->) and the bandwidth for that ordering. all items must be separated from their neighbors by exactly one space. if more than one ordering produces the same bandwidth, then choose the smallest in lexicographic ordering, that is the one that wowould appear first in an alphabetic listing.

Sample input

A:FB;B:GC;D:GC;F:AGH;E:HD#

Sample output

A B C F G D H E -> 3


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.