-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCayKhungTheoDFS.cpp
60 lines (58 loc) · 1.02 KB
/
CayKhungTheoDFS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
#define ft first
#define sd second
#define pii pair<int, int>
#define toint(a) a-'0'
#define all(a) a.begin(), a.end()
#define endl '\n'
using namespace std;
using ll = long long;
const int MOD = 1e9+7;
const int maxn = 1e5+1;
int n, m, s, used[maxn];
vector <int> adj[maxn];
vector <pii> T;
void inp(){
cin >> n >> m >> s;
for(int i = 1; i <= n; i++){
adj[i].clear();
}
for(int i = 0; i < m; i++){
int x, y; cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
memset(used, 0, sizeof(used));
T.clear();
}
void DFS(int u){
used[u] = 1;
for(int v : adj[u]){
if(!used[v]){
T.push_back({u, v});
DFS(v);
}
}
}
void solve(){
DFS(s);
if(T.size() != n-1) cout << -1 << endl;
else{
for(pii x : T){
cout << x.ft << " " << x.sd << endl;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int test; cin >> test;
while(test--)
{
inp();
solve();
}
return 0;
}
/*Code by: Tran Quang Huy*/