用dfs数出图的连通分量数。
#include<cstdio>
#include<algorithm>
using namespace std;
int cityNum, roadNum, checkNum;
bool map[1000][1000];
bool visited[1000];
int lostCity;
void dfs(int cur) {
visited[cur] = true;
for (int i = 1;i <= cityNum;i++) {
if (map[cur][i] && i != lostCity && !visited[i])
dfs(i);
}
}
int main() {
scanf("%d %d %d", &cityNum, &roadNum, &checkNum);
for (int i = 0;i < roadNum;i++) {
int c1, c2;
scanf("%d %d", &c1, &c2);
map[c1][c2] = map[c2][c1] = true;
}
for (int i = 0;i < checkNum;i++) {
scanf("%d", &lostCity);
fill(visited, visited + 1000, false);
int count = 0;
for (int j = 1;j <= cityNum;j++) {
if (j == lostCity)
continue;
if (!visited[j]) {
dfs(j);
count++;
}
}
printf("%d\n", count - 1);
}
}