forked from wolverinn/Waking-Up
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hkex_layers.cpp
64 lines (63 loc) · 1.38 KB
/
hkex_layers.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
61
62
63
64
/*
建图,bfs即可, 主要难点在于接收输入(未知行,每行未知个int)
*/
#include <iostream>
#include <string>
#include <bits/stdc++.h>
using namespace std;
int main() {
int start;
cin >> start;
cin.ignore();
unordered_map<int, set<int>> rec;
string line;
while (true) {
getline(cin, line);
if (line.empty()) {
break;
}
stringstream ss(line);
int first;
ss >> first;
if (rec.find(first) == rec.end()) {
rec[first] = set<int>();
}
int num;
while (ss >> num) {
rec[first].insert(num);
if (rec.find(num) == rec.end()) {
rec[num] = set<int>();
}
rec[num].insert(first);
}
}
unordered_map<int, bool> visit;
visit[start] = true;
queue<int> now;
now.push(start);
while (!empty(now)) {
int n = now.size();
vector<int> nowSeq = vector<int>();
for (int i = 0; i < n; i++) {
int nowNum = now.front();
nowSeq.push_back(nowNum);
set<int> next = rec[nowNum];
for (const int& element: next) {
if (visit[element] != true) {
now.push(element);
visit[element] = true;
}
}
now.pop();
}
sort(nowSeq.begin(), nowSeq.end());
for (int i = 0; i < nowSeq.size(); i++) {
if (i == n-1) {
cout << nowSeq[i] << endl;
} else {
cout << nowSeq[i] << " ";
}
}
}
return 0;
}