Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the elements in V, then the bandwidth of a node v is defined as the maximum distance in the ordering between v 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 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 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.
A:FB;B:GC;D:GC;F:AGH;E:HD #
A B C F G D H E -> 3
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;
#define CLR(x,y) memset((x),(y),sizeof((x)))
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);(x)--)
#define FOR2(x,y,z) for((x)=(y);(x)<(z);(x)++)
#define FORD2(x,y,z) for((x)=(y);(x)>=(z);(x)--)
char str[8*10],ch[10];
int G[29][29],vis[28],n,minres,res[10],a[10],g[10][10];
//G保存原图,g保存更正后的图,ch保存出现的节点的字母,a临时保存dfs过程中的顺序,
//res当前最优的顺序,minres当前最优的答案,
void read_in(){
//清理
CLR(g,0);CLR(G,0);CLR(vis,0);n = 0;
char *p = str;
int u,v,len = strlen(str);
//后面不上一个 ";',方便后面书写
str[len] = ';';
str[len + 1] = '\0';
//保存原图,并记录出现的字母
while(*p){
u = *p - 'A';
if(!vis[u]){
vis[u] = 1;
ch[n++] = *p;
}
++p;++p;
while(*p != ';'){
int v = *p - 'A';
if(!vis[v]){
vis[v] = 1;
ch[n++] = *p;
}
G[u][v] = G[v][u] = 1;
p++;
}
p++;
}
sort(ch,ch + n);
//矫正编号
FOR(i,0,27){
FOR(j,0,27){
if(G[i][j]){
FOR(k,0,n){
if(i == ch[k] - 'A') u = k;
if(j == ch[k] - 'A') v = k;
}
g[u][v] = 1;
}
}
}
}
int count(int cur){
int u = a[cur],v;
FOR(i,0,cur){
v = a[i];
if(g[u][v]) return cur - i;
}
return 0;
}
void dfs(int cur,int curmax){
if(cur == n){
minres = curmax;
memcpy(res,a,sizeof(a));
return ;
}
FOR(i,0,n){
if(!vis[i]){
a[cur] = i;
vis[i] = 1;
int tmp = max(curmax,count(cur));
if(tmp < minres) dfs(cur + 1,tmp);
vis[i] = 0;
}
}
}
int main(){
while(gets(str) && str[0] != '#'){
read_in();
minres = n + 1;
CLR(vis,0);
dfs(0,0);
FOR(i,0,n) printf("%c ",ch[res[i]]);
printf("-> %d\n",minres);
}
return 0;
} |