-
Notifications
You must be signed in to change notification settings - Fork 1
/
tarzan.cpp
217 lines (171 loc) · 4.24 KB
/
tarzan.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//============================================================================
// Instituto Superior Tecnico
// Projecto Analise e Sintese de Algoritmos
//
// Date: : Marco, 2014
// Author : Vasco Loureiro, 70993
// Author : Nuno Cameira, 69769
// Version : 1.0.0.0
// Description :
//============================================================================
#include <vector>
#include <stdio.h>
#include <iostream>
using namespace std;
class Node {
private:
int _id;
int _index;
int _low;
int _scc;
bool _visited;
vector<int> _followers;
public:
Node() :
_id(-1), _index(-1), _low(-1), _scc(-1), _visited(false), _followers(
0) {
}
Node(int id, int index, int max) :
_id(id), _index(index), _low(index), _scc(-1), _visited(false), _followers(
0) {
}
int getFollowers() {
return _followers.size();
}
void addFollower(int follower) {
_followers.push_back(follower);
}
std::vector<int>::iterator firstFollower() {
return _followers.begin();
}
std::vector<int>::iterator lastFollower() {
return _followers.end();
}
int getLow() const {
return _low;
}
void setLow(int low) {
_low = low;
}
int getIndex() const {
return _index;
}
void setIndex(int index) {
_index = index;
}
int getID() const {
return _id;
}
bool isVisited() {
return _visited;
}
void setVisited(bool visited) {
_visited = visited;
}
void setSCC(int val) {
_scc = val;
}
int getSCC() {
return _scc;
}
bool operator!=(const Node &other) const {
return (_id != other._id);
}
};
class Stack {
private:
vector<int> visitedOrder;
vector<int> visitedOrderSorted;
public:
Stack(int numberOfElements) :
visitedOrder(0), visitedOrderSorted(numberOfElements) {
}
void push_back(int val) {
visitedOrder.push_back(val);
visitedOrderSorted[val] = 1;
}
int pop_back() {
int lastElement = visitedOrder.back();
visitedOrder.pop_back();
visitedOrderSorted[lastElement] = 0;
return lastElement;
}
bool contains(int val) {
return (visitedOrderSorted[val] == 1);
}
};
void tarzan_visit(int pos, Node graph[], int *index, Stack *stack,
int groups[]) {
Node *visitingNode = &graph[pos];
visitingNode->setIndex(*index);
visitingNode->setLow(*index);
visitingNode->setVisited(true);
*index += 1;
stack->push_back(visitingNode->getID());
// Consider successors of visitingNode
Node *nextFollower = 0;
for (std::vector<int>::iterator it = visitingNode->firstFollower();
it != visitingNode->lastFollower(); ++it) {
nextFollower = &graph[*it];
if (!nextFollower->isVisited()) {
tarzan_visit(*it, graph, index, stack, groups);
visitingNode->setLow(
min(visitingNode->getLow(), nextFollower->getLow()));
} else if (stack->contains(*it)) {
visitingNode->setLow(
min(visitingNode->getLow(), nextFollower->getIndex()));
}
} //For: NextChild
// If visitingNode is a root node, pop the stack and generate a SCC
if (visitingNode->getLow() == visitingNode->getIndex()) {
groups[0]++;
Node *followerChild = 0;
Node *follower = 0;
int groupSize = 0;
bool isSelfish = true;
do {
follower = &graph[stack->pop_back()];
follower->setSCC(groups[0]);
for (std::vector<int>::iterator it = follower->firstFollower();
it != follower->lastFollower(); ++it) {
followerChild = &graph[*it];
if (followerChild->getSCC() == -1)
continue; //Child is in Stack
if (followerChild->getSCC() != follower->getSCC()) {
isSelfish = false;
break; //At least one element has an outside reference
}
}
groupSize++;
} while (follower != visitingNode);
if (isSelfish)
groups[2]++;
groups[1] = max(groups[1], groupSize);
}
}
int main() {
int nNodes = 0;
int nConnections = 0;
scanf("%i %i", &nNodes, &nConnections);
Node graph[nNodes];
for (int i = 0; i < nNodes; i++) {
graph[i] = Node(i, 0, nNodes);
}
int leader = 0;
int nextFollower = 0;
while (scanf("%i %i", &leader, &nextFollower) != EOF) {
graph[leader - 1].addFollower(nextFollower - 1);
}
int level = 0;
Stack stack = Stack(nNodes);
int groups[3] = { 0, 0, 0 };
for (int i = 0; i < nNodes; i++) {
if (!graph[i].isVisited()) {
tarzan_visit(i, graph, &level, &stack, groups);
}
}
cout << groups[0] << endl;
cout << groups[1] << endl;
cout << groups[2] << endl;
return 0;
}