Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///componente conexe
- #include <iostream>
- #include <fstream>
- using namespace std;
- int c[100], a[100][100], n;
- bool viz[100];
- void citire()
- {
- ifstream f("graf.in");
- f>>n;
- for (int i=1; i<=n; ++i)
- for(int j=1; j<=n; j++)
- f>>a[i][j];
- }
- int main()
- {
- int p, b, start, j, nc=0, u,x;
- citire();
- for(start=1; start<=n; ++start)
- {
- if(!viz[start])
- {
- p=u=1;
- nc++;
- c[1]=start;
- viz[start]=true;
- while(u>=p)
- {
- x=c[p];
- for(j=1; j<=n; ++j)
- if(a[j][x]&&!viz[j])
- {
- viz[j]=true;
- ++u;
- c[u]=j;
- }
- p++;
- }
- cout<<"componenta conexa "<<nc<<endl;
- for (j=1; j<=u; j++)
- cout<<c[j]<<" ";
- cout<<endl<<endl;
- }
- }
- cout<<"Graful are "<<nc<<" elemente conexe.\n";
- }
- /*graf.in
- 6
- 0 1 1 0 0 0
- 1 0 0 0 0 0
- 1 0 0 0 0 0
- 0 0 0 0 0 1
- 0 0 0 0 0 0
- 0 0 0 1 0 0
- */
Advertisement
Add Comment
Please, Sign In to add comment