Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- int n,m,x,a[100][100],viz[100],c[100];
- void citire()
- {
- ifstream f ("componenteconexe.in");
- f>>n>>m;
- for(int q=1;q<=m;q++)
- {
- int i,j;
- f>>i>>j;
- a[i][j]=a[j][i]=1;
- }
- }
- void BF(int x)
- {
- int p,u,v;
- p=u=1;
- c[p]=x;
- cout<<c[p]<<" ";
- viz[x]=1;
- while(p<=u)
- {
- v=c[p];
- for(int i=1;i<=n;i++)
- if(a[i][v]==1 && viz[i]==0)
- {
- viz[i]=1;
- u++;
- c[u]=i;
- cout<<c[u]<<" ";
- }
- p++;
- }
- cout<<endl;
- }
- int main()
- {
- citire();
- for(int i=1;i<=n;i++)
- if(viz[i]==0)
- BF(i);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement