Cod sursa(job #3370756)
| Utilizator | Data | 27 octombrie 2026 11:04:51 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.54 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n,x,i,j,m;
vector<int> G[10005];
vector<bool> v;
void DFS(int X)
{
v[X]=1;
fout<<X<<" ";
for(auto I=G[X].begin();I!=G[X].end();I++)
if(v[*I]==0)
DFS(*I);
}
int main()
{
fin>>n>>m>>x;
v=vector<bool>(n+1);
while(fin>>i>>j)
{
G[i].push_back(j);
G[j].push_back(i);
}
for(int i=1;i<=n;++i)
sort(G[i].begin(),G[i].end());
DFS(x);
return 0;
}