Pagini recente » Cod sursa (job #1676781) | Cod sursa (job #846038) | Cod sursa (job #3259242) | Cod sursa (job #508110) | Cod sursa (job #1338326)
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int V,E,maxi;
int visited[100001];
vector< vector<int> >graph;
void dfs(int curr)
{
visited[curr]=1;
for(int w=0;w<graph[curr].size();w++)
if(!visited[graph[curr][w]])
dfs(graph[curr][w]);
}
int main()
{
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
cin>>V>>E;
graph.resize(V+1);
for(int x=0;x<E;x++)
{
int temp1,temp2;
cin>>temp1>>temp2;
graph[temp1].push_back(temp2);
graph[temp2].push_back(temp1);
}
for(int x=1;x<=V;x++)
{
if(!visited[x])
{
dfs(x);
maxi++;
}}
cout<<maxi<<endl;
return 0;
}