-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzzle_12_2.cc
65 lines (55 loc) · 1.59 KB
/
puzzle_12_2.cc
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
#include <algorithm>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <vector>
using Children = std::vector<int>;
using Map = std::map<int, Children>;
int main()
{
Map map;
std::string line;
while (std::getline(std::cin, line)) {
std::vector<std::string> words;
boost::algorithm::split(
words, line, boost::is_any_of(", "), boost::algorithm::token_compress_on);
auto it = words.begin();
int k = std::stoi(&(*it++)[0]);
if (*it++ != "<->") {
std::cerr << "ERROR: " << line << "\n";
return 1;
}
for (; it != words.end(); ++it) {
map[k].push_back(std::stoi(&(*it)[0]));
}
}
int groups = 0;
for (; !map.empty();) {
std::list<int> queue;
std::set<int> zero_group;
for (const auto& m : map) {
if (m.second.size() < 2) {
queue.push_back(m.first);
zero_group.insert(m.first);
break;
}
}
for (; !queue.empty();) {
int cur = queue.front();
queue.pop_front();
const auto& next = map[cur];
for (auto i : next) {
if (zero_group.insert(i).second) {
queue.push_back(i);
}
}
map.erase(cur);
}
++groups;
std::cout << "zero_group: " << zero_group.size() << "\n";
}
std::cout << "groups: " << groups << "\n";
}