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>
- using namespace std;
- unordered_map<int, unordered_set<int>> graph;
- unordered_map<int, int> path;
- void bfs(){
- queue<int> next;
- next.push(0);
- path[0] = 0;
- while(!next.empty()) {
- int current = next.front();
- next.pop();
- for(auto i : graph[current]){
- if(path.count(i) == 0){
- path[i] = path[current] + 1;
- next.push(i);
- }
- }
- }
- }
- int main() {
- int k, a, b;
- cin >> k;
- for(int i = 0; i < k; i++){
- cin >> a >> b;
- graph[a].insert(b);
- graph[b].insert(a);
- }
- bfs();
- int sum = 0;
- for(auto i : path)
- sum += i.second;
- cout << sum;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment