|
|
back to boardCould anyone tell me what's wrong on my program??(+) Thanks:). #include<iostream.h> unsigned char n; char a[200][200]; unsigned char p[200]; char used[200]; void print(unsigned char i, unsigned char meta, unsigned char step) { if (i!=meta) print(p[i],meta, step+1); else cout<<(short)step<<" "; cout<<(short)(i+1)<<" "; } void visit(unsigned char i) { unsigned char j; if (used[i]) { print(p[i],i,1); cout<<endl; return; } used[i]=1; for (j=0; j<n; j++) if (a[i][j]) { a[i][j]=0; a[j][i]=0; p[j]=i; visit(j); } } void main() { int i, j, rings, how; long m; cin>>n>>m; for (i=0; i<n; i++) for (j=0; j<n; j++) a[i][j]=0; while (m>0) { cin>>i>>j; --i,--j; a[i][j]=1; a[j][i]=1; --m; } rings=0; for (i=0; i<n; i++) { how=0; for (j=i+1; j<n; j++) how+=(short)a[i][j]; if (how>0) rings=rings+how-1; } cout<<rings<<endl; for (j=0; j<n; j++) used[j]=0; for (i=0; i<n; i++) if (!used[i]) { for (j=0; j<n; j++) p[j]=-1; visit(i); } } Note, that may be 1 or MORE component of graph. > Thanks:). > > #include<iostream.h> > > unsigned char n; > char a[200][200]; > unsigned char p[200]; > char used[200]; > > void print(unsigned char i, unsigned char meta, unsigned char step) > { > if (i!=meta) print(p[i],meta, step+1); > else cout<<(short)step<<" "; > cout<<(short)(i+1)<<" "; > } > > void visit(unsigned char i) > { > unsigned char j; > if (used[i]) > { > print(p[i],i,1); > cout<<endl; > return; > } > used[i]=1; > for (j=0; j<n; j++) > if (a[i][j]) > { > a[i][j]=0; > a[j][i]=0; > p[j]=i; > visit(j); > } > } > > void main() > { > int i, j, rings, how; > long m; > cin>>n>>m; > for (i=0; i<n; i++) > for (j=0; j<n; j++) a[i][j]=0; > while (m>0) > { > cin>>i>>j; > --i,--j; > a[i][j]=1; > a[j][i]=1; > --m; > } > rings=0; > for (i=0; i<n; i++) > { > how=0; > for (j=i+1; j<n; j++) > how+=(short)a[i][j]; > if (how>0) rings=rings+how-1; > } > cout<<rings<<endl; > for (j=0; j<n; j++) used[j]=0; > for (i=0; i<n; i++) > if (!used[i]) > { > for (j=0; j<n; j++) p[j]=-1; > visit(i); > } > } |
|
|