Login
Register
Problem list
Online status
Huhu_Miao
:
2024-12-31 23:26:33
/** * author: Huhu_Miao * created: 2024.12.31 23:24:00 (UTC+8) * 并查集卡常并非刚需,DFS暴力亲测可过! **/ #include
#include
#include
void dfs(const auto g, auto& vis, int id){ vis[id] = true; for(const auto neighbor : g[id]){ if(!vis[neighbor]) dfs(g,vis,neighbor); } } void solve(){ int n , m ; std::cin >> n >> m; std::vector
> g(n+1); std::deque
vis(n+1,false); for(int i = 0 ; i < m ; i++){ int from , to; std::cin >> from >> to; g[from].push_back(to); g[to].push_back(from); } int cnt = 0; for(int i = 1 ; i < n+1 ; i++){ if(!vis[i]){ dfs(g,vis,i); cnt++; } } std::cout << cnt << '\n'; } int main(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int T; std::cin>>T; while(T--) solve(); return 0; }
Post Your Comment