国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

【codeforce】744A 并查集

2019-11-14 11:27:49
字體:
供稿:網(wǎng)友

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?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.

ExampleInput
4 1 21 31 2Output
2Input
3 3 121 21 32 3Output
0Note

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個是政府所在城市,在滿足每兩個結(jié)點間只有一條道路,且政府城市間沒有道路的條件下,問最多可以加多少條道路。   在已給出的圖中,有三種情況:1.有城市結(jié)點的圖2.點數(shù)最多的有城市節(jié)點的圖3.沒有城市結(jié)點的集合其中,有政府結(jié)點的集合構(gòu)成完全圖,可連接的變數(shù)為num*(num-1)/2;(完全圖:每兩個點之間都有一條邊,若有n個結(jié)點,可連接n*(n-1)/2條邊)沒有政府點的集合也可以構(gòu)成完全圖,若想連接邊最多,可以將無政府點集合與最多點的有政府集合連接再構(gòu)成完全圖最后,可加邊數(shù)=所有點完全圖邊數(shù)-已有的m條邊或者:可加邊數(shù)= 所有點集完全圖的邊數(shù) + 無政府點集的點數(shù) * 點數(shù)最多的有政府點集的點數(shù) -已有的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)]++;//記錄最初每個圖集合中的點數(shù)		int Max=0,sum=0,ss=n;		for(int i=1;i<=k;i++){			num[s[i]]=num[find(s[i])];			//政府所在集合的點數(shù)等于其根節(jié)點所在集合的點數(shù) 			Max=max(Max,num[s[i]]);			 ss-=num[s[i]];//減去有政府點的集合			 sum+=(num[s[i]])*(num[s[i]]-1)/2;			 //每個有政府的集合可以連接的條數(shù) 		}		sum+=(ss+Max)*(ss+Max-1)/2;		//無政府點和最大有政府集合構(gòu)成完全圖 		sum-=Max*(Max-1)/2+m;		//最大點數(shù)集合構(gòu)成完全圖連接邊數(shù)重復(fù)計算了,減去 		 PRintf("%d/n",sum);	}	return 0;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 晴隆县| 奇台县| 长兴县| 白玉县| 二连浩特市| 磐安县| 固镇县| 崇文区| 清水河县| 晋州市| 高阳县| 临城县| 浏阳市| 石阡县| 江永县| 黄浦区| 松原市| 秦皇岛市| 临洮县| 绿春县| 湘乡市| 仁化县| 顺义区| 当涂县| 林周县| 文昌市| 鹤岗市| 潼南县| 闻喜县| 南开区| 鄂托克旗| 双鸭山市| 沧州市| 锦屏县| 尚志市| 隆德县| 宣化县| 深泽县| 呼和浩特市| 昭苏县| 盖州市|