[UVA] 193 - Graph Coloring@Morris' Blog|PChome Online 人新台
2012-02-29 12:15:51| 人3,224| 回1 | 上一篇 | 下一篇

[UVA] 193 - Graph Coloring

0 收藏 0 0 站台

 Graph Coloring 

You are to write a program that tries to find an optimal coloring for a given graph. Colors are applied to the nodes of the graph and the only available colors are black and white. The coloring of the graph is called optimal if a maximum of nodes is black. The coloring is restricted by the rule that no two connected nodes may be black.

  figure22
Figure: An optimal graph with three black nodes

Input and Output

The graph is given as a set of nodes denoted by numbers tex2html_wrap_inline33 , tex2html_wrap_inline35 , and a set of undirected edges denoted by pairs of node numbers tex2html_wrap_inline37 , tex2html_wrap_inline39 . The input file contains m graphs. The number m is given on the first line. The first line of each graph contains n and k, the number of nodes and the number of edges, respectively. The following k lines contain the edges given by a pair of node numbers, which are separated by a space.

The output should consists of 2m lines, two lines for each graph found in the input file. The first line of should contain the maximum number of nodes that can be colored black in the graph. The second line should contain one possible optimal coloring. It is given by the list of black nodes, separated by a blank.

Sample Input

1 6 8 1 2 1 3 2 4 2 5 3 4 3 6 4 6 5 6

Sample Output

3 1 4 5


做法 :
此外, 做法生出的是字典序最小的 !

#include <stdio.h>
#include <string.h>
int map[101][101], mt[101];
int tmp[101], Ans[101], maxL;
int cant[101];
int T, n, m;
void DFS(int node, int choose, int notchoose) {
if(choose + notchoose == n || node == n+1) {
if(choose > maxL) {
maxL = choose;
int i;
for(i = 0; i < maxL; i++)
Ans[i] = tmp[i];
}
return;
}

if(cant[node] == 0) {
int i, newNot = notchoose;
int reduce[n], idx = 0;
for(i = 0; i < mt[node]; i++) {
if(cant[map[node][i]] == 0) {
cant[map[node][i]] = 1, newNot++;
reduce[idx++] = map[node][i];
}
}
tmp[choose] = node;
DFS(node+1, choose+1, newNot);
for(i = 0; i < idx; i++)
cant[reduce[i]] = 0;
}
DFS(node+1, choose, notchoose+(!cant[node]));
}
int main() {
int i, x, y;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &n, &m);
memset(mt, 0, sizeof(mt));
memset(cant, 0, sizeof(mt));
for(i = 0; i < m; i++) {
scanf("%d %d", &x, &y);
map[x][mt[x]++] = y;
map[y][mt[y]++] = x;
}
maxL = 0;
DFS(1, 0, 0);
printf("%d\n", maxL);
for(i = 0; i < maxL-1; i++)
printf("%d ", Ans[i]);
printf("%d\n", Ans[i]);
}
return 0;
}

台: Morris
人(3,224) | 回(1)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA] 1099 - Sharing Chocolate
此分上一篇:[UVA] 11456 - Trainsorting

xem phim online
nice article...
2017-10-03 14:10:55
是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86