Pagini recente » Cod sursa (job #36491) | Cod sursa (job #113032) | Cod sursa (job #1804890) | Cod sursa (job #2741885) | Cod sursa (job #652509)
Cod sursa(job #652509)
#include <fstream>
#define gL 100001
#define useL 100001
using namespace std;
ifstream in;
ofstream out;
struct graf
{
int nod;
graf *link;
}*g[gL];
int use[useL];
inline void add_edge(int x,int y)
{
graf *p=new graf;
p->nod=y;
p->link=g[x];
g[x]=p;
}
inline void DFS(int nod)
{
use[nod]=1;
for(graf *p=g[nod];p;p=p->link)
if(!use[p->nod]) DFS(p->nod);
}
int main()
{
int M,N,cnt=0,x,y;
in.open("dfs.in");
in>>N>>M;
for(;M--;)
{
in>>x>>y;
add_edge(x,y);
add_edge(y,x);
}
in.close();
for(int i=1;i<=N;++i)
if(!use[i]) DFS(i),++cnt;
out.open("dfs.out");
out<<cnt<<'\n';
out.close();
return 0;
}