Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- using namespace std;
- int a[101][101], n,m,succ[101], pred[101],ctc[101],nrctc;
- void citire()
- {
- int i,x,y;
- cin>>n>>m;
- for(i=1; i<=m; i++)
- {
- cin>>x>>y;
- a[x][y]=1;
- }
- }
- void DFS1(int start)
- {
- succ[start]=1;
- for(int i=1; i<=n; i++)
- {
- if(a[start][i] and !succ[i])
- DFS1(i);
- }
- }
- void DFS(int start)
- {
- pred[start]=1;
- for(int i=1; i<=n; i++)
- {
- if(a[i][start] and !pred[i])
- DFS(i);
- }
- }
- void componente()
- {
- for(int i=1; i<=n; i++)
- {
- if(ctc[i]==0)
- {
- DFS1(i); DFS(i);
- nrctc++;
- for(int j=1; j<=n; j++)
- {
- if(succ[j] && pred[j])
- ctc[j]=nrctc;
- succ[j]=0; pred[j]=0;
- }
- }
- }
- }
- int main()
- {
- citire();
- componente();
- cout<<nrctc;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement