CodeForces - 744A 并查集
Hongcow is ruler of the world. As ruler of the world, he wants to make it easier for people to travel by road within their own countries.
The world can be modeled as an undirected graph with n nodes and m edges. k of the nodes are home to the governments of the k countries that make up the world.
There is at most one edge connecting any two nodes and no edge connects a node to itself. Furthermore, for any two nodes corresponding to governments, there is no path between those two nodes. Any graph that satisfies all of these conditions isstable.
Hongcow wants to add as many edges as possible to the graph while keeping it stable. Determine the maximum number of edges Hongcow can add.
Input
The first line of input will contain three integers n, m and k (1 ≤ n ≤ 1 000, 0 ≤ m ≤ 100 000, 1 ≤ k ≤ n) — the number of vertices and edges in the graph, and the number of vertices that are homes of the government.
The next line of input will contain k integers c1, c2, ..., ck (1 ≤ ci ≤ n). These integers will be pairwise distinct and denote the nodes that are home to the governments in this world.
The following m lines of input will contain two integers ui and vi (1 ≤ ui, vi ≤ n). This denotes an undirected edge between nodes ui and vi.
It is guaranteed that the graph described by the input is stable.
Output
Output a single integer, the maximum number of edges Hongcow can add to the graph while keeping it stable.
解题思路:

#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 1005;
long long f[maxn],is_gmt[maxn],num[maxn],gmt[maxn];
int get_f(int a)
{
if(f[a]!=a)
return f[a]=get_f(f[a]);
return f[a];
}
int main()
{
long long n,m,k,maxg,home,mg,ans;
while(cin>>n>>m>>k)
{
ans = maxg = home = 0;
for(int i=1;i<=n;i++)
f[i]=i;
for(int i=1;i<=n;i++)
num[i]=1;
memset(is_gmt,0,sizeof(is_gmt));
for(int i=1;i<=k;i++)
{
cin>>gmt[i];
is_gmt[gmt[i]]=1;
}
int a,b,fa,fb;
for(int i=1;i<=m;i++)
{
cin >> a >> b;
fa = get_f(a);
fb = get_f(b);
if(is_gmt[fa])//有国家把国家当老大
f[fb]=fa;
else
f[fa]=fb;
}
for(int i=1;i<=n;i++)
{
if(f[i]!=i)
num[get_f(i)]++;//num[f[i]]++;是错的,因为还有一段未更新;更新每个集合的元素数量
}
for(int i=1;i<=k;i++)
{
if(num[gmt[i]]>maxg)
{
maxg = num[gmt[i]];
mg = gmt[i];//找到元素最多的国家
}
}
for(int i=1;i<=n;i++)
{
if(f[i]==i)//从各个集合中找不是国家的
{
if(!is_gmt[get_f(i)])
home+=num[i];
}
}
num[mg]+=home;//把home加入元素最多的国家。
for(int i=1;i<=k;i++)
ans+=num[gmt[i]]*(num[gmt[i]]-1)/2;//n个点两两相连可以连n*(n-1)条边
cout << ans - m << endl;
}
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 1005;
long long f[maxn],is_gmt[maxn],num[maxn],gmt[maxn];
int get_f(int a)
{
if(f[a]!=a)
return f[a]=get_f(f[a]);
return f[a];
}
int main()
{
long long n,m,k,add1,add2,add3,maxg,home;
while(cin>>n>>m>>k)
{
add1 = add2 = add3 = maxg = home = 0;
for(int i=1;i<=n;i++)
f[i]=i;
for(int i=1;i<=n;i++)
num[i]=1;
memset(is_gmt,0,sizeof(is_gmt));
for(int i=1;i<=k;i++)
{
cin>>gmt[i];
is_gmt[gmt[i]]=1;
}
int a,b,fa,fb;
for(int i=1;i<=m;i++)
{
cin >> a >> b;
fa = get_f(a);
fb = get_f(b);
if(fa!=fb)
{
if(is_gmt[fa])
{
f[fb]=fa;
num[fa]+=num[fb];
}
else
{
f[fa]=fb;
num[fb]+=num[fa];
}
}
}
for(int i=1;i<=k;i++)
{
add1+=num[gmt[i]]*(num[gmt[i]]-1)/2;
maxg = max(num[gmt[i]],maxg);
}
for(int i=1;i<=n;i++)
{
if(!is_gmt[get_f(i)])
home++;
}
add2 = home*(home-1)/2;
add3 = home * maxg;
cout << add1 + add2 + add3 - m << endl;
}
return 0;
}

浙公网安备 33010602011771号