Pagini recente » Cod sursa (job #254059) | Cod sursa (job #256762) | Cod sursa (job #2032701) | Cod sursa (job #535677) | Cod sursa (job #447394)
Cod sursa(job #447394)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on April 28, 2010, 4:55 PM
*/
#include <stack>
#include <vector>
#include <cstdlib>
#include <fstream>
#define Nmax 100011
/*
*
*/
using namespace std;
bool uz[Nmax];
stack< int > S;
vector< int > G[Nmax];
vector< int >::const_iterator it, iend;
inline void DFS( int x )
{
while( true )
{
uz[x]=true;
for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
if( !uz[*it] )
break;
if( it >= iend )
break;
S.push(x);
x=*it;
}
}
int main(int argc, char** argv)
{
int N, M, x, y, z;
ifstream in( "dfs.in" );
for( in>>N>>M; M; --M )
{
in>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
for( y=0, x=1; x <= N; ++x )
if( !uz[x] )
{
++y;
z=x;
while( true )
{
DFS(z);
if( S.empty() )
break;
x=S.top(); S.pop();
}
}
ofstream out( "dfs.out" );
out<<y<<'\n';
return (EXIT_SUCCESS);
}