Pagini recente » Cod sursa (job #30512) | Monitorul de evaluare | Cod sursa (job #111426) | Cod sursa (job #1526607) | Cod sursa (job #878917)
Cod sursa(job #878917)
#include <cstdio>
#include <vector>
const int MAX_SIZE(100001);
int n, m, components;
std::vector<int> graph [MAX_SIZE];
bool mark [MAX_SIZE];
inline void read (void)
{
std::freopen("dfs.in","r",stdin);
std::scanf("%d %d",&n,&m);
int x, y;
for (int counter(0) ; counter < m ; ++counter)
{
std::scanf("%d %d",&x,&y);
graph[x].push_back(y);
graph[y].push_back(x);
}
std::fclose(stdin);
}
inline void print (void)
{
std::freopen("dfs.out","w",stdout);
std::printf("%d\n",components);
std::fclose(stdout);
}
void DepthFirstSearch (int node)
{
mark[node] = true;
for (int j(0), size(graph[node].size()) ; j < size ; ++j)
if (!mark[graph[node][j]])
DepthFirstSearch(graph[node][j]);
}
int main (void)
{
read();
for (int node(1) ; node <= n ; ++node)
if (!mark[node])
{
DepthFirstSearch(node);
++components;
}
print();
return 0;
}