-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathStrongly_connected_component.cpp
82 lines (69 loc) · 1.65 KB
/
Strongly_connected_component.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
/* nuttela - Soham Chakrabarti */
/* problem Link - https://www.hackerearth.com/practice/algorithms/graphs/strongly-connected-components/practice-problems/algorithm/magical-islands/description/ */
/* Solution Link - GIVEN BELOW */
/* problem Link - https://www.hackerearth.com/practice/algorithms/graphs/strongly-connected-components/practice-problems/algorithm/a-walk-to-remember-qualifier2/ */
/* Solution - https://www.hackerearth.com/submission/39856526/ */
#include <bits/stdc++.h>
using namespace std;
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL)
#define all(v) v.begin(),v.end()
#define pb push_back
#define FR(i,j,k) for (ll i=j;i<k;++i)
#define FRR(i,j,k) for (ll i=j;i>=k;--i)
#define ff first
#define ss second
typedef long long int ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
const int N = 1e5+5;
const int M = 101;
const int inf = INT_MAX;
const int MOD = 1e7+9;
int n,m,p,q;
vector<int> G1[N],G2[N];
vector<int> node,lis;
bool v[N];
void dfs1(int u)
{
v[u]=1;
for(auto &x:G1[u]){
if(!v[x])dfs1(x);
}
node.pb(u);
}
void dfs2(int u)
{
v[u]=1;
lis.pb(u);
for(auto &x:G2[u]){
if(!v[x])dfs2(x);
}
}
int main() {
IO;
cin>>n>>m;
FR(i,0,m)
{
cin>>p>>q;
--p,--q;
G1[p].pb(q);
G2[q].pb(p);
}
memset(v,0,sizeof(v));
FR(i,0,n){
if(!v[i])dfs1(i);
}
int out=0;
memset(v,0,sizeof(v));
reverse(all(node));
for(int x:node){
lis.clear();
if(!v[x]){
dfs2(x);
out=max(out,(int)lis.size());
}
}
cout<<out;
return 0;
}