Advertisement
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,nc=0,u;
- 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];
- }
- void BF(int start)
- {
- int i, x,p;
- p=u=1;
- c[p]=start;
- viz[start]=true;
- nc++;
- while(p<=u)
- {
- x=c[p];
- for(i=1; i<=n; i++)
- if(a[i][x]==1 && viz[i]==false)
- {
- viz[i]=true;
- u++;
- c[u]=i;
- }
- p++;
- }
- }
- int main()
- {
- int p, b, start, i,x;
- citire();
- for(start=1; start<=n; ++start)
- {
- if(!viz[start])
- {
- BF(start);
- cout<<"componenta conexa "<<nc<<endl;
- for (i=1; i<=u; i++)
- cout<<c[i]<<" ";
- cout<<endl<<endl;
- }
- }
- cout<<"Graful are "<<nc<<" elemente conexe.\n";
- }
- /*graf.in
- 7
- 0 0 1 1 0 0 0
- 0 0 0 0 0 1 0
- 1 0 0 0 0 0 0
- 1 0 0 0 0 0 1
- 0 0 0 0 0 0 0
- 0 1 0 0 0 0 0
- 0 0 0 1 0 0 0
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement