Pagini recente » Cod sursa (job #1700312) | Cod sursa (job #516595) | Cod sursa (job #129070) | Cod sursa (job #2676764) | Cod sursa (job #3228849)
#include <bits/stdc++.h>
using namespace std;
class Task {
public:
void solve() {
read_input();
print_output(get_result());
}
private:
// numarul maxim de noduri
static constexpr int NMAX = (int)1e5 + 5; // 10^5 + 5 = 100.005
// n = numar de noduri, m = numar de muchii/arce
int n, m;
// adj[node] = lista de adiacenta a nodului node
// exemplu: daca adj[node] = {..., neigh, ...} => exista arcul (node, neigh)
vector<int> adj[NMAX];
void read_input() {
ifstream fin("sortaret.in");
fin >> n >> m;
for (int i = 1, x, y; i <= m; i++) {
fin >> x >> y; // arc (x, y)
adj[x].push_back(y);
}
fin.close();
}
void dfs(vector<int> &topsort,int node,vector<int> &visited){
visited[node]=1;
for(auto neight : adj[node]){
if(visited[neight]==0)
dfs(topsort,neight,visited);
}
topsort.push_back(node);
}
vector<int> get_result() {
// TODO: Faceti sortarea topologica a grafului stocat cu liste de adiacenta din adj.
// *******
// ATENTIE: nodurile sunt indexate de la 1 la n.
// *******
vector<int> topsort;
vector<int>visited(n+1,0);
for(int i=1;i<=n;i++)
if(visited[i]==0)
dfs(topsort,i,visited);
reverse(topsort.begin(),topsort.end());
return topsort;
}
void print_output(const vector<int>& topsort) {
ofstream fout("sortaret.out");
for (auto node : topsort) {
fout << node << ' ';
}
fout << '\n';
fout.close();
}
};
// [ATENTIE] NU modifica functia main!
int main() {
// * se aloca un obiect Task pe heap
// (se presupune ca e prea mare pentru a fi alocat pe stiva)
// * se apeleaza metoda solve()
// (citire, rezolvare, printare)
// * se distruge obiectul si se elibereaza memoria
auto* task = new (nothrow) Task(); // hint: cppreference/nothrow
if (!task) {
cerr << "new failed: WTF are you doing? Throw your PC!\n";
return -1;
}
task->solve();
delete task;
return 0;
}