Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include <unordered_map>
- #include <unordered_set>
- #include <queue>
- #include <set>
- using namespace std;
- unordered_map<int, unordered_set<int>> graph;
- set<int> result;
- bool visited[100000] {false};
- void bfs(int start, int distance){
- queue<pair<int, int>> next;
- next.push({start, 0});
- visited[start] = true;
- while(!next.empty()){
- auto curr = next.front();
- next.pop();
- auto neighbours = graph[curr.first];
- if(curr.second == distance - 1) {
- for(auto i : neighbours) {
- if(!visited[i]){
- result.insert(i);
- }
- }
- }
- else {
- for(auto i : neighbours) {
- if(!visited[i]){
- next.push({i, curr.second + 1});
- visited[i] = true;
- }
- }
- }
- }
- }
- int main() {
- int n, m, k, a, b;
- cin >> n >> m >> k;
- for(int i = 0; i < m; i++){
- cin >> a >> b;
- graph[a].insert(b);
- graph[b].insert(a);
- }
- if(k == 0){
- cout << n;
- return 0;
- }
- bfs(n, k);
- if(result.empty()){
- cout << -1;
- return 0;
- }
- for(int i : result)
- cout << i << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment