-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschlnet.cpp
144 lines (133 loc) · 3.49 KB
/
schlnet.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
/*
ID: paulius10
PROG: schlnet
LANG: C++
*/
#include <algorithm>
#include <fstream>
#include <stack>
#include <vector>
using namespace std;
void strongconnect(int v, int N, int &index, int &n, int indices[],
int lowlink[], bool on_stack[], stack<int> &S,
vector<bool> edge[], int scc[]) {
// set the depth index of v to the smallest unused index
indices[v] = index;
lowlink[v] = index++;
S.push(v);
on_stack[v] = true;
// consider successors of v
for (int w = 0; w < N; w++) {
if (!edge[v][w]) continue;
if (indices[w] == -1) {
// successor w has not yet been visited; recurse on it
strongconnect(w, N, index, num_sccs, indices, lowlink, on_stack, S,
edge, scc);
if (lowlink[w] < lowlink[v]) lowlink[v] = lowlink[w];
} else if (on_stack[w] && indices[w] < lowlink[v]) {
// successor w is in stack S and hence in the current SCC
lowlink[v] = indices[w];
}
}
// if v is a root node, pop the stack and generate an SCC
if (lowlink[v] == indices[v]) {
int w;
do {
w = S.top();
S.pop();
on_stack[w] = false;
scc[w] = n;
} while (w != v);
n++;
}
}
/*
Groups all vertices into strongly connected components (by modifying scc[])
and returns the total number of SCC's.
*/
int tarjan(int N, vector<bool> edge[], int scc[]) {
int index = 0, n = 0, indices[N], lowlink[N];
bool on_stack[N];
for (int i = 0; i < N; i++) {
indices[i] = lowlink[i] = -1;
on_stack[i] = false;
}
stack<int> S;
for (int v = 0; v < N; v++) {
if (indices[v] == -1)
strongconnect(v, N, index, n, indices, lowlink, on_stack, S, edge, scc);
}
return n;
}
/*
Returns an uncovered vertex with the smallest in-degree.
*/
int argmin(int in[], bool covered[], int n) {
int m = -1;
for (int i = 0; i < n; i++)
if (!covered[i] && (m == -1 || in[i] < in[m])) m = i;
return m;
}
/*
Marks all vertices reachable from i as covered.
*/
void dfs(int i, int n, vector<bool> adjacent[], bool covered[]) {
covered[i] = true;
for (int j = 0; j < n; j++) {
if (adjacent[i][j] && !covered[j]) dfs(j, n, adjacent, covered);
}
}
int main() {
int N;
ifstream fin("schlnet.in");
fin >> N;
vector<bool> edge[N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) edge[i].push_back(false);
for (int i = 0; i < N; i++) {
while (true) {
int j;
fin >> j;
if (j == 0) break;
edge[i][j - 1] = true;
}
}
fin.close();
int scc[N], n = tarjan(N, edge, scc);
ofstream fout("schlnet.out");
if (n == 1) {
fout << 1 << endl << 0 << endl;
} else {
// construct a new graph with SCC's instead of vertices
vector<bool> adjacent[n];
bool covered[n];
// keep track of in- and out-degrees
int in[n], out[n];
for (int i = 0; i < n; i++) {
in[i] = out[i] = 0;
covered[i] = false;
for (int j = 0; j < n; j++) adjacent[i].push_back(false);
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (edge[i][j] && scc[i] != scc[j]) {
if (!adjacent[scc[i]][scc[j]]) {
out[scc[i]]++;
in[scc[j]]++;
adjacent[scc[i]][scc[j]] = true;
}
}
}
}
int c;
for (c = 0; ; c++) {
int i = argmin(in, covered, n);
if (i == -1) break;
dfs(i, n, adjacent, covered);
}
fout << c << endl << max(count(in, in + n, 0), count(out, out + n, 0))
<< endl;
}
fout.close();
return 0;
}