-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojeto.cpp
89 lines (82 loc) · 2.34 KB
/
projeto.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
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
void le_input(vector<vector<int>>& grafo, vector<vector<int>> >){
int n, m;
if(!scanf("%d %d", &n, &m))
exit(EXIT_FAILURE);
grafo = vector<vector<int>>(n, vector<int>());
gt = vector<vector<int>>(n, vector<int>());
for(int i = 0; i < m; i++){
int x, y;
if(!scanf("%d %d", &x, &y))
exit(EXIT_FAILURE);
grafo[x - 1].push_back(y - 1);
gt[y - 1].push_back(x - 1);
}
}
int max_jumps(vector<vector<int>> &grafo, vector<vector<int>> >) {
int n = grafo.size();
stack<int> S, LT;
vector<bool> marked = vector<bool>(n, false);
for (int i = 0; i < n; i++) {
if(marked[i])
continue;
marked[i] = true;
S.push(i);
while(!S.empty()) {
int v = S.top();
bool d = true;
for(unsigned int i = 0; i < grafo[v].size(); i++) {
if(marked[grafo[v][i]])
continue;
d = false;
marked[grafo[v][i]] = true;
S.push(grafo[v][i]);
break;
}
if(d) {
S.pop();
LT.push(v);
}
}
}
vector<int> C = vector<int>(n, -1);
int num_scc = -1, max_jumps = 0;
vector<int> max_jumps_scc;
while(!LT.empty()) {
int v = LT.top();
LT.pop();
if(marked[v]) {
num_scc++;
marked[v] = false;
C[v] = num_scc;
S.push(v);
max_jumps_scc.push_back(0);
}
while(!S.empty()) {
int t = S.top();
S.pop();
marked[t] = false;
for(unsigned int i = 0; i < gt[t].size(); i++) {
if(marked[gt[t][i]]) {
S.push(gt[t][i]);
C[gt[t][i]] = num_scc;
}
else if(C[gt[t][i]] != num_scc) {
max_jumps_scc[num_scc] = max(max_jumps_scc[num_scc], max_jumps_scc[C[gt[t][i]]] + 1);
}
}
}
if(max_jumps_scc[num_scc] > max_jumps) {
max_jumps = max_jumps_scc[num_scc];
}
}
return max_jumps;
}
int main() {
vector<vector<int>> grafo, gt;
le_input(grafo, gt);
printf("%d\n", max_jumps(grafo, gt));
}