Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int n, m;
- vector <vector<int>> g;
- vector <bool> used;
- vector <int> q;
- int main() {
- cin >> n >> m;
- g.resize(n);
- used.resize(n, false);
- 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);
- }
- q.push_back(0);
- used[0] = true;
- while(!q.empty()){
- int v = q.back();
- bool fl = true;
- int j = 0;
- while (j < g[v].size()){
- if (!used[g[v][j]]){
- fl = false;
- break;
- }
- else j++;
- }
- if (!fl){
- q.push_back(g[v][j]);
- cout << v+1 <<' '<<g[v][j]+1 <<'\n';
- used[g[v][j]] = true;
- }
- else q.pop_back();
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment