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.
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:
For the second sample test, the graph looks like this:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int f[1000];
int find(int x)//寻根
{
if(f[x]==x)
return x;
else return find(f[x]);
}
void unionn(int a,int b)
{
int x=find(a);
int y=find(b);
if(x!=y)
f[x]=y;
}
int main()
{
int n,m,k,x,y;
int s[1000],num[1000];
while(~scanf("%d%d%d",&n,&m,&k)){
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++)
f[i]=i;//初始化
for(int i=1;i<=k;i++)
scanf("%d",&s[i]);//输入代表政府的点
for(int i=0;i<m;i++){
scanf("%d%d",&x,&y);
unionn(x,y);//连接加入同一集合
}
for(int i=1;i<=n;i++)
num[find(i)]++;//记录最初每个图集合中的点数
int Max=0,sum=0,ss=n;
for(int i=1;i<=k;i++){
num[s[i]]=num[find(s[i])];
//政府所在集合的点数等于其根节点所在集合的点数
Max=max(Max,num[s[i]]);
ss-=num[s[i]];//减去有政府点的集合
sum+=(num[s[i]])*(num[s[i]]-1)/2;
//每个有政府的集合可以连接的条数
}
sum+=(ss+Max)*(ss+Max-1)/2;
//无政府点和最大有政府集合构成完全图
sum-=Max*(Max-1)/2+m;
//最大点数集合构成完全图连接边数重复计算了,减去
printf("%d\n",sum);
}
return 0;
}