Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define si(n) scanf("%d",&n)
- #define MAX 100005
- bool vis[MAX];
- vector<int>temp;
- vector<int>graph[MAX],rev_graph[MAX];
- void dfs(int now)
- {
- if(vis[now])
- return;
- vis[now]=true;
- for(int i=0;i<graph[now].size();i++){
- dfs(graph[now][i]);
- }
- temp.push_back(now);
- }
- void rev_dfs(int now){
- if(vis[now])
- return ;
- vis[now]=true;
- for(int i=0;i<rev_graph[now].size();i++)
- rev_dfs(rev_graph[now][i]);
- }
- int main()
- {
- //freopen("input.txt","r",stdin);
- int i,n,m,scc=0;
- si(n);si(m);
- for(i=0;i<m;i++){
- int u,v;
- si(u);si(v);
- graph[u].push_back(v);
- rev_graph[v].push_back(u);
- }
- memset(vis,false,sizeof(vis));
- for(i=1;i<=n;i++){
- if(!vis[i])dfs(i);
- }
- memset(vis,false,sizeof(vis));
- for(i=temp.size()-1;i>=0;i--){
- int now=temp[i];
- if(vis[now])
- continue;
- rev_dfs(now);
- scc++;
- }
- printf("total scc = %d",scc);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement