Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <numeric>
- #include <set>
- #include <queue>
- #include <stack>
- #include <vector>
- #include <climits>
- #include <string>
- #include <cstdio>
- #include <cmath>
- #define task "TESTCODE"
- using namespace std;
- struct T
- {
- int node, id;
- };
- vector<vector<T>> Adj;
- bool b[400001];
- int n, m, x, y;
- vector<int> V;
- void read()
- {
- cin >> n >> m;
- Adj.resize(n + 1);
- for (int i = 1; i <= m; ++ i)
- {
- cin >> x >> y;
- Adj[x].push_back({y, i});
- Adj[y].push_back({x, i});
- b[i] = true;
- }
- }
- void solve()
- {
- V.push_back(1);
- while (!V.empty())
- {
- int u = V.back();
- bool deleted = false;
- while (!Adj[u].empty() && b[Adj[u].back().id] == false)
- {
- Adj[u].pop_back();
- }
- if (!Adj[u].empty())
- {
- V.push_back(Adj[u].back().node);
- b[Adj[u].back().id] = false;
- Adj[u].pop_back();
- }
- if (u == V.back())
- {
- cout << V.back() << ' ';
- V.pop_back();
- }
- }
- }
- /// TLE, I don't know why, how the da fuq did that happen
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- //freopen(task".INP", "r", stdin);
- //freopen(task".OUT", "w", stdout);
- read();
- solve();
- }
Add Comment
Please, Sign In to add comment