140 - Bandwidth

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=76

Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and anordering on the elements in V, then thebandwidth of a nodev is defined as the maximum distance in the ordering betweenv and 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 many ways, two of which are illustrated 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 by `;'. Each record will consist of a node name (a single upper case character in the the range `A' to `Z'), followed by a `:' and at least one of its neighbours. 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 neighbours 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 would 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



枚举全排列,遇到nowmin > _min的情况就剪掉。


完整代码:

/*0.016s*/

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;

char s[100];
int permu[30], ans[30], _min, c;
bool m[30][30], node[30];

void analyze(char *s)///初始化
{
	int state = 0;
	for (int i = 0; s[i]; ++i)
	{
		if (isalpha(s[i]))
		{
			if (!node[s[i] & 31])
			{
				node[s[i] & 31] = true;
				++c;
			}
			if (!state)
				state = s[i] & 31;
			else
			{
				m[state][s[i] & 31] = 1;
				m[s[i] & 31][state] = 1;
			}
		}
		else if (s[i] == ';' && state)
			state = 0;
	}
}

void dfs(int cur, int max)
{
    int i,j;
	if (cur == c) ///fin
	{
		if (_min > max)
		{
			for (int i = 0; i < c; ++i)
				ans[i] = permu[i];
			_min = max;
		}
		return;
	}
	for (i = 1; i <= 26; ++i)
		if (node[i])
		{
			for (j = cur - max - 1; j >= 0; --j)
				if (m[i][permu[j]])
					max = cur - j;
			if (max > _min) return;///剪枝
			node[i] = false;
			permu[cur] = i;
			dfs(cur + 1, max);
			node[i] = true;
		}
}

int main()
{
	while (gets(s), s[0] != '#')
	{
		memset(node, 0, sizeof(node));
		memset(m, 0, sizeof(m));
		c = 0;
		analyze(s);
		_min = -1u >> 1;
		for (int i = 1; i <= 26; ++i)
			if (node[i])
			{
				node[i] = false;///表示该值已在全排列中
				permu[0] = i;
				dfs(1, 0);
				node[i] = true;
			}
		for (int i = 0; i < c; ++i)
			printf("%c ", ans[i] + 'A' - 1);
		printf("-> %d\n", _min);
	}
	return 0;
}