include iostream include vector include queue using namespace std int

 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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int n, m, s = 0;
cin >>n >>m;
vector <vector <int> > g(n);
for (int i = 0; i < n; ++i) {
int from, to;
cin >>from >>to;
from--; to--;
g[from].push_back(to);
g[to].push_back(from);
}
queue<int> q;
q.push(s);
vector<bool> used(n);
vector<int> d(n), p(n);
used[s] = true;
p[s] = -2;
while (!q.empty()) {
int v = q.front();
q.pop();
for (int i = 0; i<g[v].size(); ++i) {
int to = g[v][i];
if (!used[to]) {
used[to] = true;
q.push(to);
d[to] = d[v] + 1;
p[to] = v;
}
}
}
for (int i = 0; i < n; ++i) {
cout << '\"' << i + 1 << "\" -> \"" << p[i] + 1 << "\"" <<endl;
}
return 0;
}