Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<string>
- #include<set>
- #include<algorithm>
- #define ll long long
- #define ld long double
- #define pb push_back
- using namespace std;
- vector<vector<int>> g;
- vector<bool> used;
- vector<pair<int, int>> answ;
- void dfs(int h) {
- used[h] = true;
- for (auto e : g[h]) {
- if (!used[e]) {
- answ.push_back({h, e});
- dfs(e);
- }
- }
- }
- int main() {
- int n, m;
- cin>>n>>m;
- g.resize(n);
- for (int i = 0; i < m; i++) {
- int x, y;
- cin>>x>>y;
- x--, y--;
- g[x].push_back(y);
- g[y].push_back(x);
- }
- used.assign(n, false);
- dfs(0);
- for (auto e : answ) {
- cout << e.first + 1 << ' ' << e.second + 1 << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment