Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
- using namespace std;
- int n, m, d, tem[1100][1110] = {}, ans[1100] = {};
- bool dd[1100] = {};
- vector<int> v[1100] = {};
- queue<int> q;
- void dfs(int a) {
- dd[a] = true;
- cout << a << " ";
- for (int i = 0; i < v[a].size(); i++) {
- if (dd[v[a][i]] == false) dfs(v[a][i]);
- }
- }
- void bfs(int a) {
- cout << a << " ";
- dd[a] = true;
- q.push(a);
- while (!q.empty()) {
- int node = q.front();
- q.pop();
- for (int i = 0; i < v[node].size(); i++) {
- if (dd[v[node][i]] == false) {
- dd[v[node][i]] = true;
- cout << v[node][i] << " ";
- q.push(v[node][i]);
- }
- }
- }
- }
- int main() {
- fastio;
- cin >> n >> m >> d;
- for (int i = 0; i < m; i++) {
- int a, b;
- cin >> a >> b;
- v[a].push_back(b);
- v[b].push_back(a);
- }
- for (int i = 1; i <= n; i++) std::sort(v[i].begin(), v[i].end());
- dfs(d);
- cout << "\n";
- for (int i = 1; i <= n; i++) dd[i] = false;
- bfs(d);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement