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

首頁 > 學院 > 開發設計 > 正文

【codeforce】744A 并查集

2019-11-14 11:29:34
字體:
來源:轉載
供稿:網友

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個是政府所在城市,在滿足每兩個結點間只有一條道路,且政府城市間沒有道路的條件下,問最多可以加多少條道路。   在已給出的圖中,有三種情況: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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 佛山市| 葵青区| 武宣县| 兴义市| 高阳县| 荔浦县| 高青县| 丹阳市| 芒康县| 永吉县| 韶关市| 米脂县| 兴国县| 三原县| 博乐市| 北宁市| 固安县| 海阳市| 德江县| 甘孜县| 洛隆县| 西乌珠穆沁旗| 天等县| 吉安县| 炎陵县| 通海县| 明光市| 花莲县| 萍乡市| 丹凤县| 辽阳县| 汝州市| 开阳县| 墨竹工卡县| 南安市| 泗阳县| 克拉玛依市| 怀来县| 石渠县| 阿拉善盟| 区。|