【codeforce】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 nm and k (1 ≤ n ≤ 1 0000 ≤ m ≤ 100 0001 ≤ 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.

Example
Input
4 1 2
1 3
1 2
Output
2
Input
3 3 1
2
1 2
1 3
2 3
Output
0
Note

For the first sample test, the graph looks like this:

Vertices 1 and 3 are special. The optimal solution is to connect vertex 4 to vertices 1 and 2. This adds a total of 2 edges. We cannot add any more edges, since vertices 1 and 3 cannot have any path between them.

For the second sample test, the graph looks like this:

We cannot add any more edges to this graph. Note that we are not allowed to add self-loops, and the graph must be simple.
题意:有一个地方,总共有n个城市,m条道路,其中有k个是政府所在城市,在满足每两个结点间只有一条道路,且政府城市间没有道路的条件下,问最多可以加多少条道路。
   在已给出的图中,有三种情况:
1.有城市结点的图
2.点数最多的有城市节点的图
3.没有城市结点的集合
其中,有政府结点的集合构成完全图,可连接的变数为num*(num-1)/2;
(完全图:每两个点之间都有一条边,若有n个结点,可连接n*(n-1)/2条边)
没有政府点的集合也可以构成完全图,若想连接边最多,可以将无政府点集合与最多点的有政府集合连接再构成完全图
最后,可加边数=所有点完全图边数-已有的m条边
或者:可加边数= 所有点集完全图的边数 + 无政府点集的点数 * 点数最多的有政府点集的点数 - 已有的m条边
code:
#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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值