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

首頁 > 學院 > 開發(fā)設計 > 正文

【codeforce】744A 并查集

2019-11-14 10:56:43
字體:
來源:轉載
供稿:網(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個是政府所在城市,在滿足每兩個結點間只有一條道路,且政府城市間沒有道路的條件下,問最多可以加多少條道路。   在已給出的圖中,有三種情況:1.有城市結點的圖2.點數(shù)最多的有城市節(jié)點的圖3.沒有城市結點的集合其中,有政府結點的集合構成完全圖,可連接的變數(shù)為num*(num-1)/2;(完全圖:每兩個點之間都有一條邊,若有n個結點,可連接n*(n-1)/2條邊)沒有政府點的集合也可以構成完全圖,若想連接邊最多,可以將無政府點集合與最多點的有政府集合連接再構成完全圖最后,可加邊數(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;		//無政府點和最大有政府集合構成完全圖 		sum-=Max*(Max-1)/2+m;		//最大點數(shù)集合構成完全圖連接邊數(shù)重復計算了,減去 		 PRintf("%d/n",sum);	}	return 0;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 崇信县| 克什克腾旗| 钟祥市| 周口市| 新源县| 达州市| 潜江市| 体育| 余姚市| 新田县| 绥宁县| 肇东市| 金秀| 四会市| 岳西县| 兰坪| 上思县| 禄丰县| 洛川县| 建昌县| 建始县| 正阳县| 乐至县| 双柏县| 加查县| 夏河县| 鄂伦春自治旗| 洱源县| 柳州市| 阳西县| 都匀市| 福建省| 叶城县| 潞西市| 诸暨市| 梓潼县| 宝应县| 商城县| 濮阳市| 桐柏县| 手游|