-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnected_components.cpp
More file actions
57 lines (54 loc) · 1.15 KB
/
connected_components.cpp
File metadata and controls
57 lines (54 loc) · 1.15 KB
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#define MAXN 100
using namespace std;
int n ;
vector < vector<int> > g (MAXN) ;
bool used [ MAXN ] ;
vector < int > comp ;
void dfs ( int v ) {
used [ v ] = true ;
comp. push_back ( v ) ;
for ( size_t i = 0 ; i < g [ v ] . size ( ) ; ++ i ) {
int to = g [ v ] [ i ] ;
if ( ! used [ to ] )
dfs ( to ) ;
}
}
void find_comps ( ) {
for ( int i = 0 ; i < n ; ++ i )
used [ i ] = false ;
for ( int i = 0 ; i < n ; ++ i )
if ( ! used [ i ] ) {
comp. clear ( ) ;
dfs ( i ) ;
cout << "Component:" ;
for ( size_t j = 0 ; j < comp. size ( ) ; ++ j )
cout << ' ' << comp [ j ] ;
cout << endl ;
}
}
int main()
{
n=3;
g[0].push_back(1);
g[0].push_back(2);
g[1].push_back(0);
g[2].push_back(0);
g[2].push_back(1);
find_comps();
//cout<<time_in[0]<<" "<<time_in[1]<<" "<<time_in[2]<<endl;
//cout<<time_out[0]<<" "<<time_out[1]<<" "<<time_out[2]<<endl;
//cout<<d[0]<<" "<<d[1]<<" "<<d[2]<<endl;
return 0;
return 0;
}