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 is stable.
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.
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 a single integer, the maximum number of edges Hongcow can add to the graph while keeping it stable.
4 1 2 1 3 1 2
2
3 3 1 2 1 2 1 3 2 3
0
For the first sample test, the graph looks like this:
![](https://i-blog.csdnimg.cn/blog_migrate/f88b23cb7c40e9bc374758e7a320edcf.png)
For the second sample test, the graph looks like this:
![](https://i-blog.csdnimg.cn/blog_migrate/496c0abaa0d3f80f195ddff41a0ebb43.png)
题目大意:
给你一个无向图,其中有N个点,M条无向边,已知有K个重要点,其中任意两个重要点如果都不能直接或者间接连接(就是任意两个重要点不连通),那么就称作这个图当前是稳定的。
初始的图一定是稳定的,问我们还最多可以添加多少条边,能够使得图还是稳定的。
思路:
1、首先我们使用并查集将所有联通块都处理出来。
2、因为我们每个联通块中最多只有一个重要点,那么我们肯定一个联通块我们可以对其进行添加边至:tmpn*(tmpn-1)【这里tmpn表示这个联通块中点的个数,即我们将联通块变成一个五向完全图】;那么我们对于没有重要点的联通块,我们可以将其和有重要点的联通块连接在一起,然而我们对应要选择一个有重要点的联通块连接在一起,又因为我们连接在一起之后肯定是将其变成无向完全分图的,那么我们肯定是要找包含重要点的点数最多的联通块来与其合并,那么我们:
①我们使用并查集将所有联通块都处理出来
②找到具有点数最多的包含重要点的联通块,记录点数maxn.
③对于其他包含重要点的联通块,肯定边数需要增加至tmpn*(tmpn-1);
④对于不包含重要点的联通块,将其与②中的联通块连接在一起,这部分的点数和我们记做cnt.
⑤那么ans=(cnt+maxn)*(cnt+maxn-1)/2+Σ其他各个具有重要点的联通块的点数tmp*(tmp-1)/2;
Ac代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int mmm[1005];
int f[1005];
int flag[1005];
int vis[1005];
int sum[1005];
int bian[1005];
int n,m,k;
int find(int a)
{
int r=a;
while(f[r]!=r)
r=f[r];
int i=a;
int j;
while(i!=r)
{
j=f[i];
f[i]=r;
i=j;
}
return r;
}
void merge(int a,int b)
{
int A,B;
A=find(a);
B=find(b);
bian[A]++;
if(A!=B)
{
f[B]=A;
sum[A]+=sum[B];
flag[A]+=flag[B];
}
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
memset(mmm,0,sizeof(mmm));
memset(vis,0,sizeof(vis));
memset(flag,0,sizeof(flag));
memset(sum,0,sizeof(sum));
for(int i=0;i<k;i++)
{
int x;
scanf("%d",&x);
mmm[x]=1;
flag[x]=1;
}
for(int i=1;i<=n;i++)
{
f[i]=i;
sum[i]=1;
}
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
merge(x,y);
}
int output=0;
int maxn=0;
for(int i=1;i<=n;i++)
{
if(f[i]==i&&flag[i]==1)
{
maxn=max(maxn,sum[i]);
}
}
int cnt=0;
int tmp=0;
for(int i=1;i<=n;i++)
{
if(f[i]==i)
{
if(flag[i]==1)
{
if(sum[i]==maxn)
{
if(tmp==0)
{
tmp=1;
}
else
{
output+=sum[i]*(sum[i]-1)/2;
}
}
else
{
output+=sum[i]*(sum[i]-1)/2;
}
}
else
{
cnt+=sum[i];
}
}
}
output+=(cnt+maxn)*(cnt+maxn-1)/2;
printf("%d\n",output-m);
}
}