-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC2017_12.cpp
68 lines (58 loc) · 1.2 KB
/
AoC2017_12.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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <map>
#include <set>
using namespace std;
map <int, set<int>> M; // M[vertex] = set of its neighbors
set <int> S; // set of verticed visited by DFS algorithm
// DFS algorithm
void search_connected_area(int n)
{
if(S.count(n)>0)
return;
S.insert(n);
for (int i:M[n])
search_connected_area(i);
}
int main()
{
ifstream in;
string line, str;
int a,b;
int groups=0;
// reading input
in.open("input.txt");
while (getline(in, line))
{
if (!line.empty())
{
istringstream istr(line);
istr >> a;
M[a] = set<int>();
istr.ignore(4);
while (istr >> b)
{
M[a].insert(b);
istr.ignore(1);
}
}
}
in.close();
// solving part 1
search_connected_area(0);
++groups;
cout << "First part: " << S.size() << endl;
// solving part 2
for (auto kvp : M)
{
if (S.count(kvp.first)==0)
{
search_connected_area(kvp.first);
++groups;
}
}
cout << "Second part: " << groups << endl;
return 0;
}