Pagini recente » Cod sursa (job #332560) | Cod sursa (job #2660748) | Cod sursa (job #3128516) | Cod sursa (job #2136859) | Cod sursa (job #1084939)
#include <fstream>
using namespace std;
ifstream f("dfs.in"); ofstream g("dfs.out");
int c[1000001], i,j,m,n,v1,v2,nr,nr1,t;
void conex(int x,int y){
if ((c[x]==0)&&(c[y]!=0)) c[x]=c[y]; else
if ((c[x]!=0)&&(c[y]==0)) c[y]=c[x]; else
if ((c[x]==0)&&(c[y]==0)) {
(nr++);
c[x]=nr;
c[y]=c[x];
} else
if ((c[x]!=0)&&(c[y]!=0)&&(c[x]!=c[y])) {
nr1++; t=c[x];
for (j=1;j<=n;j++) if (c[j]==t) c[j]=c[y];
}
}
int main(){
f>>n>>m;
for (i=1;i<=m;i++){
f>>v1>>v2;
conex(v1,v2);
}
for (i=1;i<=n;i++) if (c[i]==0) nr++;
g<<nr-nr1;
}