标签:ring mes fun size UNC ++ set push amp
题意:
思路:计算连通块。
代码:
#include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn=1005; bool vis[maxn]; vector<int> graph[maxn]; int n,m,k; void dfs(int v) { vis[v]=true; for(auto u:graph[v]) if(!vis[u]) dfs(u); } int func(int v) { //cut memset(vis,false,sizeof(vis)); vis[v]=true; //calculate the number of connected block int cnt=0; for(int i=1;i<=n;i++){ if(!vis[i]){ dfs(i); cnt++; } } return cnt-1; } int main() { scanf("%d%d%d",&n,&m,&k); int u,v; for(int i=0;i<m;i++){ scanf("%d%d",&u,&v); graph[u].push_back(v); graph[v].push_back(u); } int query; while(k--){ scanf("%d",&query); printf("%d\n",func(query)); } return 0; }
标签:ring mes fun size UNC ++ set push amp
原文地址:https://www.cnblogs.com/kkmjy/p/9580766.html