Pagini recente » Cod sursa (job #697938) | Cod sursa (job #1723181) | Cod sursa (job #534831) | Cod sursa (job #753352) | Cod sursa (job #2425143)
#include <iostream>
#include <fstream>
#include <vector>
#include<stack>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
vector<int> viz;
int n, m, s,d,nr=0;
bool ok;
vector<int > a[100005];
stack<int > stiva;
void citire()
{
int x, y, i;
f >> n >>m;
for(i=0; i<=n; i++)
viz.push_back(0);
while(f>>x>>y)
{
a[x].push_back(y);
a[y].push_back(x);
}
}
void dfs(int X)
{
viz[X] = 1;
for(unsigned int itr =0 ; itr < a[X].size(); itr ++)
{
int temp=a[X][itr];
if( viz[temp] == 0) dfs(temp);
}
stiva.push(X);
}
int main()
{
citire();
for( int i=1; i<=n; i++)
{
if(viz[i]==0)
dfs(i);
}
while(!stiva.empty())
{
g<<stiva.top()<< " ";
stiva.pop();
}
return 0;
}