Pagini recente » Cod sursa (job #22117) | Cod sursa (job #137850) | Cod sursa (job #2115536) | Cod sursa (job #29912) | Cod sursa (job #1084914)
#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)) {
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+1;
}