qfpjm @ 2021-08-09 16:22:46
我该怎样优化?
#include <bits/stdc++.h>
using namespace std;
struct planet
{
int x, y;
}s[10000005];
int n, m, k, parent[10000005];
map<int, bool> mp;
int find(int v1)
{
while (v1 != parent[v1])
{
v1 = parent[v1];
}
return v1;
}
void _union(int v1, int v2)
{
int p1 = find(v1);
int p2 = find(v2);
if (p1 != p2)
{
parent[p1] = p2;
}
}
bool isSame(int v1, int v2)
{
return find(v1) == find(v2);
}
int count_p()
{
int sum = 0;
for (int i = 0 ; i < n ; i ++)
{
if (parent[i] == i && mp[i])
{
sum ++;
}
}
return sum;
}
void parent_i()
{
for (int i = 0 ; i < n ; i ++)
{
parent[i] = i;
}
}
int main()
{
cin >> n >> m;
for (int i = 0 ; i < n ; i ++)
{
mp[i] = true;
}
parent_i();
for (int i = 1 ; i <= m ; i ++)
{
cin >> s[i].x >> s[i].y;
_union(s[i].x, s[i].y);
}
cout << count_p() << endl;
cin >> k;
for (int i = 1 ; i <= k ; i ++)
{
parent_i();
int p;
cin >> p;
mp[p] = false;
for (int j = 1 ; j <= m ; j ++)
{
if (mp[s[j].x] && mp[s[j].y])
{
_union(s[j].x, s[j].y);
}
}
cout << count_p() << endl;
}
return 0;
}