Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://lqdoj.edu.vn/problem/lexipath
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1e5 + 5;
- vector<int> g[N];
- bool visited[N];
- int n, m, s, t;
- vector<int> path;
- // DFS(x) == true nếu x == t hoặc DFS(con của x) == t
- bool DFS(int u) {
- visited[u] = true;
- if (u == t) {
- path.push_back(u);
- return true;
- }
- for (int v : g[u]) {
- if (!visited[v] && DFS(v) == true) {
- path.push_back(u);
- return true;
- }
- }
- return false;
- }
- int main() {
- #ifdef LOCAL
- freopen("in.txt", "r", stdin);
- #else
- freopen("LEXIPATH.inp", "r", stdin);
- freopen("LEXIPATH.out", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cin >> n >> m >> s >> t;
- for (int i = 1; i <= m; i++) {
- int u, v;
- cin >> u >> v;
- g[u].push_back(v);
- }
- for (int i = 1; i <= n; i++) {
- sort(g[i].begin(), g[i].end());
- }
- DFS(s);
- for (int i = (int) path.size() - 1; i >= 0; i--) {
- cout << path[i] << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment