Pagini recente » Cod sursa (job #742174) | Cod sursa (job #1744700) | Cod sursa (job #874374) | Cod sursa (job #2636819) | Cod sursa (job #1428336)
#include <iostream>
#include <fstream>
#define MAX 100005
using namespace std;
ifstream f ("dfs.in");
ofstream g ("dfs.out");
int n,m,v[MAX],x,y,k,first[MAX],last[MAX];
struct
{
int x,y,r;
}a[400005];
void comp(int x)
{
int i,y;
v[x]=1;
i=first[x];
//cout<<i<<'\n';
while (i!=0)
{
y=a[i].y;
if (v[y]==0) comp(y);
i=a[i].r;
}
}
int main()
{
f>>n>>m;
for (int i=1;i<=m;i++)
{
f>>a[2*i].x>>a[2*i].y;
a[2*i+1].x=a[2*i].y;
a[2*i+1].y=a[2*i].x;
x=a[2*i].x;
y=a[2*i].y;
if (first[x]==0) first[x]=last[x]=2*i;
else
{
a[last[x]].r=2*i;
last[x]=2*i;
}
if (first[y]==0) first[y]=last[y]=2*i+1;
else
{
a[last[y]].r=2*i+1;
last[y]=2*i+1;
}
}
for (int i=1;i<=n;i++)
{
if (v[i]==0)
{
comp(i);
k++;
}
}
g<<k;
return 0;
}