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