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 <set>
- using namespace std;
- void dfs(unordered_map<int, set<int>>& graph, vector<bool>& visited, int start){
- visited[start] = true;
- for(int neighbour : graph[start]){
- if(!visited[neighbour])
- dfs(graph, visited, neighbour);
- }
- }
- int test(){
- int x, y, a, b;
- cin >> x >> y;
- vector<bool> visited(x, false);
- unordered_map<int, set<int>> graph;
- for(int i = 0; i < y; i++){
- cin >> a >> b;
- graph[a].insert(b);
- graph[b].insert(a);
- }
- int counter = 0;
- for(int i = 0; i < x; i++){
- if(!visited[i]){
- counter++;
- dfs(graph, visited, i);
- }
- }
- return counter;
- }
- int main() {
- int t;
- cin >> t;
- for(int i = 0; i < t; i++){
- cout << test() << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment