Pagini recente » Cod sursa (job #658856) | Monitorul de evaluare | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #1427462)
#include <fstream>
#include <vector>
using namespace std;
const int MAX = 100014 ;
vector < int > gr [ MAX ] ;
int viz [ MAX ] ;
inline void dfs ( int nod )
{
viz [ nod ] = 1 ;
for ( auto x : gr [ nod ] )
if ( viz [ x ] == 0 )
dfs ( x ) ;
}
ifstream fin ( "dfs.in" ) ;
ofstream fout ( "dfs.out" ) ;
int main ( void )
{
int n , m ;
fin >> n >> m ;
for ( int i = 1 ; i <= m ; ++ i )
{
int x , y ;
fin >> x >> y ;
gr [ x ].push_back ( y ) ;
gr [ y ].push_back ( x ) ;
}
int comp = 0 ;
for ( int i = 1 ; i <= n ; ++ i )
if ( viz [ i ] == 0 )
{
++ comp ;
dfs ( i ) ;
}
fout << comp << '\n' ;
return 0;
}