- 题意求连通图
- 思路:套模板
-
#include <iostream> #include<stdio.h> #include<string.h> #include<cmath> #include<queue> using namespace std; const int maxn=1001; int n,m; int ans; int vis[maxn]; int mp[maxn][maxn]; void dfs(int s) { queue<int>q; q.push(s); vis[s]=1; int u; while(!q.empty()) { u=q.front(); q.pop(); for( int v=1; v<=n; v++) { if(mp[u][v]==0)continue; if(vis[v]==1)continue; vis[v]=1; ans++; q.push(v); } } } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(vis,0,sizeof(vis)); for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) mp[i][j]=0; while(m--) { int a,b; scanf("%d%d",&a,&b); mp[a][b]=mp[b][a]=1; } ans=0; dfs(1); cout<<ans<<endl; } return 0; }
F - Flying Safely
最新推荐文章于 2021-01-26 15:15:51 发布