-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdfs.cpp
More file actions
74 lines (66 loc) · 1.31 KB
/
dfs.cpp
File metadata and controls
74 lines (66 loc) · 1.31 KB
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
72
73
74
/*#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <list>
#include <sstream>
#include <string>
#include <queue>
#include <fstream>
using namespace std;
void dfs(int &s, vector<int> &color, list<int>* &adjLists)
{
color[s-1] = 1;
for (auto& u : adjLists[s-1])
{
if (color[u-1] == 0)
{
dfs(u, color, adjLists);
}
}
color[s-1] = 2;
}
vector<int> split(const string& s, char delimiter)
{
vector<int> words;
string word;
istringstream wordStream(s);
while (getline(wordStream, word, delimiter))
words.push_back(stoi(word));
return words;
}
int main()
{
ifstream fin("input.txt");
freopen("output.txt", "w", stdout);
int n, s; //number of nodes and starting node
string str;
getline(fin, str);
n = stoi(str);
getline(fin, str);
s = stoi(str);
//cin >> n;
//cin >> s;
//vector<int> dist(n, -1);
//vector<int> par(n, 0);
list<int>* adjLists = new list<int>[n];
vector<int> color(n, 0); // 0 - white, 1 - grey, 2 - black
for (int i = 0; i < n; ++i)
{
getline(fin, str);
vector<int> nodes;
nodes = split(str, ' ');
for (size_t j = 0; j < nodes.size(); ++j)
{
adjLists[i].push_back(nodes[j]);
//cout << nodes[j] << ' ';
}
//cout << endl;
}
fin.close();
dfs(s, color, adjLists);
for (auto& c : color)
cout << c << endl;
getchar();
return 0;
}
*/