-
Notifications
You must be signed in to change notification settings - Fork 0
/
10305OrderingTasks.cpp
67 lines (52 loc) · 1.68 KB
/
10305OrderingTasks.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
// Vasudev Vijayaraman
// 10305 - Ordering Tasks
// UVA login name - vasapp
// Data Structure required - Vector
// Tricks - Pushing into the vector correctly and traversing through it
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int>> matrix; // 2d vectors of int to push the data in
vector<bool> fountOut; // keep track if discovered or not
vector<int> answer; // sorting the top
void depthFirstAlgorithm(int first)
{
fountOut[first] = true; // set bool to true
int second;
for (int i = 0; i < matrix[first].size(); ++i) { // traverse through the
second = matrix[first][i];
if (fountOut[second] == false) // if has not found out
depthFirstAlgorithm(second); // keep traversing & recurrence
}
answer.push_back(first); // pushing back into the top
}
int main()
{
int numberofTask;
int relations;
int first;
int second;
while (cin >> numberofTask >> relations) { // reading in data
if (numberofTask == 0 || relations == 0)
break;
else {
fountOut.assign(numberofTask + 1, false); // assign a vector of size number of tasks and set all to false
matrix.resize(numberofTask + 1); // set the vector size to the number of Task
while (relations--) {
cin >> first >> second; // reading in the m and n
matrix[first].push_back(second); // pushing into a 2d vector
}
for (int i = 1; i <= numberofTask; i++) {
if (fountOut[i] == false) //
depthFirstAlgorithm(i);
}
for (int i = numberofTask - 1; i >= 0; --i) { // printing from backwards
cout << answer[i] << " ";
}
cout << endl;
}
}
return 0;
}