-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathassignment1.cpp
More file actions
98 lines (83 loc) · 2.01 KB
/
assignment1.cpp
File metadata and controls
98 lines (83 loc) · 2.01 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <bits/stdc++.h>
using namespace std;
void addEdge(vector<int> adj[], int s, int d) {
adj[s].push_back(d);
}
// Print the graph for debugging purposes
void printGraph(vector<int> adj[], int V) {
for (int d = 0; d < V; ++d) {
cout << "\n Vertex "
<< d << ":";
for (auto x : adj[d])
cout << "-> " << x;
printf("\n");
}
}
void DFS(vector<int> adj[], int s, int V, int visited[], stack<int> &Stack) {
visited[s] = 1;
for (auto x : adj[s]) {
if (visited[x] == 0) {
DFS(adj, x, V, visited, Stack);
}
}
Stack.push(s);
}
int DFS2(vector<int> adj[], int s, int V, int visited[], int size) {
visited[s] = 1;
size = 1;
for (auto x : adj[s]) {
if (visited[x] == 0) {
size += DFS2(adj, x, V, visited, size);
}
}
return size;
}
int main() {
int V = 875714;
vector<int> adj[V];
vector<int> adj2[V];
// to test on small test cases, load SCC_small.txt
// and change V to 3200
ifstream file("SCC.txt");
int a, b;
while (file >> a >> b) {
addEdge(adj, a-1, b-1);
addEdge(adj2, b-1, a-1);
}
//printGraph(adj, V);
//printGraph(adj2, V);
int visited[V] = {0};
stack<int> Stack;
for (int i = 0; i < V; i++) {
if (visited[i] == 0) {
DFS(adj, i, V, visited, Stack);
}
}
for (int i = 0; i < V; i++) {
visited[i] = 0;
}
int currsize = 0;
priority_queue<int> pq;
while (!Stack.empty()) {
int s = Stack.top();
Stack.pop();
if (visited[s] == 0) {
cout << "visiting node " << s << endl;
int Size = DFS2(adj2, s, V, visited, currsize);
if (pq.size() < 5) {
pq.push(-1 * Size);
} else {
if (-1 * Size < pq.top()) {
pq.pop();
pq.push(-1 * Size);
}
}
}
currsize = 0;
}
while (!pq.empty()) {
cout << -1 * pq.top() << " ";
pq.pop();
}
cout << endl;
}