1. 程式人生 > >【codeforce】744A 並查集

【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;
}