Pagini recente » Cod sursa (job #3184150) | Cod sursa (job #2341449) | Cod sursa (job #1635892) | Cod sursa (job #1449591) | Cod sursa (job #2170752)
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
using namespace std;
const int M = 100005;
ifstream fcin("dfs.in");
ofstream fcout("dfs.out");
int main()
{
vector<int> V[M];
stack<int> st;
int n, m, x, y;
fcin >> n >> m;
for (int i = 0; i < m; ++i)
{
fcin >> x >> y;
V[x].push_back(y);
V[y].push_back(x);
}
bool visited[M];
for (int i = 1; i <= n; ++i)
visited[i] = false;
visited[1] = true;
int db = 1;
st.push(1);
for (int s = 1; s <= n; ++s)
{
if (!visited[s])
{
st.push(s);
visited[s] = true;
++db;
}
while (!st.empty())
{
int ss = st.top();
st.pop();
for (int i = 0; i < V[ss].size(); ++i)
if (!visited[V[ss][i]])
{
visited[V[ss][i]] = true;
st.push(V[ss][i]);
}
}
}
fcout << db;
}