算法:既然只有两种颜色,那么可以将所有的点分到两个集合中,假定:集合A是红色,集合B是黑色,而且相同集合中的点之间不能有边,不同集合中的点必须有边,即对于集合A中的任意一个点都能连到集合B中,但是这一点不必担心,因为这是强连通图,任意一个点都不会孤立的。只要看前一个条件了,SO:我在读入每一条边时就将两个顶点分别放在两个集合中,下次再读入一条边,如果发现有个顶点出现在集合A(B)中,就将另一个顶点放入B(A)中,如果发现有一个顶点同时出现在A和B中,就不能not bicoloring.
WA:表面上看是对的,但是例如读入<0 1> 的话,A中有【0】,B中有【1】,再读入<3 4>时,算法就会将A修改为【0,3】,B修改为【1,4】,此时就规避了A【0,4】,B【1,3】这种可能了,当然会忽略很多种可能。
WA代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70//10004Bicoloring WA //2013-4-18 /* 0 3 ..... 在出现<3,4>的时候算法中假定3涂1号色,4涂 -1号色,如果这样最终没有出现符合题意的结果 1 4 就能代表真的没有可能出现吗?显然不对,比如可能出现 4涂1号色,3涂 -1号色时出现可能情况。 */ #include<iostream> #include<vector> using namespace std; class Bicoloring{ private: vector <int> node; bool BICOLORABLE; public: void initial(); void readCase(int); void computing(); void outResult(); }; void Bicoloring::initial(){ node.clear(); BICOLORABLE = false; } void Bicoloring::readCase(int m){ int n1,n2; while(m--){ cin >> n1 >> n2; node.push_back(n1); node.push_back(n2); } } void Bicoloring::computing(){ int temp[205]={0}; int n1,n2,i; for(i = 0; i < node.size(); i=i+2){ n1 = node[i]; n2 = node[i+1]; if(temp[n1] == 0 && temp[n2] == 0){temp[n1] = 1;temp [n2] = -1;} if(temp[n1] == 0 && temp[n2]) {temp[n1] = - temp[n2];} if(temp[n2] == 0 && temp[n1]) {temp[n2] = - temp[n1];} if(temp[n1] && temp[n2]){ if(temp[n1] == temp[n2]) break; } //cout << "i = " <<i <<endl; } if(i == node.size()) BICOLORABLE = true; } void Bicoloring::outResult(){ if(BICOLORABLE) cout << "BICOLORABLE." <<endl; else cout << "NOT BICOLORABLE." <<endl; } int main(){ Bicoloring b; int n,m; cin >> n; while(n--){ if(cin >> m && m ){ b.initial(); b.readCase(m); b.computing(); b.outResult(); } } return 0; }
最后
以上就是健康奇迹最近收集整理的关于uva10004Bicoloring-WA的全部内容,更多相关uva10004Bicoloring-WA内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复