define _CRT_SECURE_NO_WARNI NGS include iostream include iomanip inclu

 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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <map>
using namespace std;
vector<vector<int> > g;
vector<int> used;
int main(){
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n, m, s;
cin >> n >> m >> s;
g.resize(n);
used.resize(n);
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
g[a - 1].push_back(b - 1);
g[b - 1].push_back(a - 1);
}
queue<int> q;
vector<int> anc(n, -1);
q.push(s - 1);
while (!q.empty()) {
int from = q.front();
used[from] = true;
q.pop();
for (int i = 0; i < g[from].size(); ++i) {
int to = g[from][i];
if (!used[to]) {
q.push(to);
anc[to] = from;
used[to] = true;
}
}
}
cout << "digraph G {" << endl;
for (int i = 0; i < n; ++i) {
cout << "\"" << anc[i] + 1<< "\"" << "->" << "\"" << i + 1 << "\"" << endl;
}
cout << "}" << endl;
return 0;
}