Pagini recente » Cod sursa (job #1593686) | Cod sursa (job #1430526) | Cod sursa (job #2601209) | Cod sursa (job #2595559) | Cod sursa (job #710599)
Cod sursa(job #710599)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
bool visited[100000];
int main()
{
int N, M;
ifstream in("dfs.in");
in >> N >> M;
vector <int> graph[100000];
stack<int> st;
for(int i = 0, a, b; i < M; ++i)
{
in >> a >> b;
graph[a-1].push_back(b-1);
graph[b-1].push_back(a-1);
}
in.close();
int con = 0;
for(int i = 0; i < N; ++i)
{
if(!visited[i])
{
st.push(i);
while(!st.empty())
{
int now = st.top();
st.pop();
visited[now] = true;
for(int i = 0; i < graph[now].size(); ++i)
{
if(!visited[graph[now][i]])
st.push(graph[now][i]);
}
}
++con;
}
}
ofstream out("dfs.out");
out << con << "\n";
out.close();
return 0;
}