不要用并查集,会超时的说...
还是深度优先搜索好用...
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
bool graph[1001][1001];
bool visited[1001];
int N,M,K;
using namespace std;
void dfs(int city,int except){
visited[city]= true;
for (int i = 1; i <= N ; ++i) {
if(graph[city][i]&&!visited[i]&&i!=except){
dfs(i,except);
}
}
}
int main() {
cin>>N>>M>>K;
for (int i = 0; i < M; ++i) {
int a,b;
cin>>a>>b;
graph[a][b]=true;
graph[b][a]=true;
}
vector<int> test;
for (int i = 0; i < K; ++i) {
int c;
cin>>c;
test.push_back(c);
}
for (int city : test) {
memset(visited,0, sizeof(visited));
int count=0;
for (int i = 1; i <=N ; ++i) {
if(!visited[i]&&city!=i) {
dfs(i, city);
count++;
}
}
cout<<count-1<<endl;
}
return 0;
}