-
Notifications
You must be signed in to change notification settings - Fork 0
/
sln.cpp
71 lines (68 loc) · 1.84 KB
/
sln.cpp
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
71
#include <iostream>
#include <fstream>
#include <vector>
#include <math.h>
const static char* usage = "%s <#cnf-file> [#sat-file]\n";
std::vector<std::string> split(const std::string& str, const std::string& delim)
{
std::vector<std::string> tokens;
size_t prev = 0, pos = 0;
do {
pos = str.find(delim, prev);
if (pos == std::string::npos) pos = str.length();
std::string token = str.substr(prev, pos-prev);
if (!token.empty()) tokens.push_back(token);
prev = pos + delim.length();
}
while (pos < str.length() && prev < str.length());
return tokens;
}
int main(int argc, char** argv)
{
if (argc != 3) {
printf(usage, argv[0]);
return 1;
}
// initilaize node& color
std::ifstream ifs_c,ifs_s;
int nodes, colors = 0;
ifs_c.open(argv[1]);
if (!ifs_c.is_open()){
std::cout << "can't open file " << argv[1] << std::endl;
return 1;
}
std::string line;
std::getline(ifs_c, line);
auto tokens = split(line, " ");
nodes = std::stoi(tokens[2]);
std::getline(ifs_c, line);
tokens = split(line, " ");
colors = tokens.size() - 1;
nodes /= colors;
std::vector<int> vars;
ifs_s.open(argv[2]);
if(!ifs_s.is_open()){
std::cout << "can't open file " << argv[2] << std::endl;
return 1;
}
std::getline(ifs_s, line);
if (line == "UNSAT") {
std::cout << "result is unsatisfiable\n";
return 1;
}
std::getline(ifs_s, line);
tokens = split(line, " ");
for (const auto& i : tokens) {
vars.push_back(std::stoi(i));
}
for (int i = 0; i < nodes; i++){
std::cout << "node " << i + 1;
for (int j = 0; j < colors; j++){
if (vars[i * colors + j] > 0){
std::cout << ": color " << j + 1 << std::endl;
break;
}
}
}
return 0;
}