Pagini recente » Cod sursa (job #1928138) | Cod sursa (job #1434257) | Cod sursa (job #77265) | Cod sursa (job #2946306) | Cod sursa (job #1667463)
#include<fstream>
#define e endl
using namespace std;
ifstream f("dfs.in");
ofstream t("dfs.out");
char sp[]=" ";
typedef struct cel
{
int info;
cel *next;
}*nodic;
bool viz[100000];
int m,n,x,y,conex;
nodic lda[100000];
void add(int x,nodic &y)
{
nodic r=new cel;
r->info=x;
r->next=y;
y=r;
}
void dfs(int x)
{
if(!viz[x]){
viz[x]=1;
for(nodic p=lda[x];p;p=p->next) dfs(p->info);
}
}
int main(){
f>>n>>m;
while(m--){
f>>x>>y;
add(x,lda[y]);
add(y,lda[x]);
}
for(int i=1;i<=n;++i)
if(!(viz[i]))
dfs(i) , conex++;
t<<conex<<e;
return 0;
}