Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #define N 101
- using namespace std;
- ifstream fin("componenteconexe.in");
- ofstream fout("componenteconexe.out");
- struct nod
- {
- int info;
- nod*urm;
- }*L[N];
- int viz[N];
- int n,m,nd;
- void Citire()
- {
- fin>>n;
- int x,y;
- nod*p;
- while(fin>>x>>y)
- {
- p=new nod;
- p->info=y;
- p->urm=L[x];
- L[x]=p;
- p=new nod;
- p->info=x;
- p->urm=L[y];
- L[y]=p; ///neorien
- }
- }
- void Sortare(int nd)
- {
- int aux;
- for(nod*p=L[nd]; p->urm; p=p->urm)
- for(nod*q=p->urm; q; q=q->urm)
- if(p->info>q->info)
- {
- aux=p->info;
- p->info=q->info;
- q->info=aux;
- }
- }
- void DFS(int nd,int cc)
- {
- nod*p;
- ///fout<<nd<<' ';
- p=L[nd];
- viz[nd]=cc;
- while(p)
- {
- if(viz[p->info]==0)
- DFS(p->info,cc);
- p=p->urm;
- }
- }
- int main()
- {
- Citire();
- for(int i=1; i<=n; i++)
- Sortare(i);
- int cc=0;
- for(int i=1; i<=n; i++)
- if(viz[i]==0)
- {
- cc++;
- DFS(i,cc);
- }
- fout<<cc<<'\n';
- for(int j=1;j<=cc;j++)
- {
- for(int i=1;i<=n;i++)
- if(viz[i]==j)fout<<i<<' ';
- fout<<'\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement