您的位置:首页 > 产品设计 > UI/UE

CodeForces 744A Hongcow Builds A Nation (并查集)

2016-12-23 20:24 459 查看
Hongcow Builds A Nation

time limit per test
 2 seconds

memory limit per test
 256 megabytes

input
 standard input

output
 standard output

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 kcountries 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 is stable.

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 n, m 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.

Examples

input
4 1 2
1 3
1 2


output
2


input
3 3 1
21 21 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.每棵树上可以添加的边数 (ran[i]*ran[i]-1)/2;
2.没有限制的两棵树之间可以相互进行连接;
3.对于有限制的树,找到有限制的树中点数最多的树,将它与没限制的树进行连接。
最后要减去已经连过的m;
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>mp;
int pre[1100],ran[1100];
int find(int x)
{
if(x==pre[x])
return x;
return pre[x]=find(pre[x]);
}
void merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
ran[fy]+=ran[fx];
}
}
int n,m,k,i,j,a,b,c[1100],gov[1100];
int main()
{
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
for(i = 0; i <= n; i++)
{
pre[i] = i;
ran[i] = 1;
gov[i]=0;
}
for(i=0;i<k;i++)
{
scanf("%d",&c[i]);
}
for(i = 0; i < m; i++)
{
scanf("%d%d",&a,&b);
merge(a,b);
}
for(i = 0; i < k; i++)
{
gov[find(c[i])]=1;//标记受限制的点
}
int sum = 0,maxx = 0;
for(i = 1; i <= n; i++)
{
if(find(i)==i)
{
if(gov[i])//找到受限制的块中,点数最多的块
{
maxx = max(ran[i] , maxx);
}
else
{
mp.push_back(ran[i]);
}
sum += ran[i]*(ran[i]-1)/2;//每一个块中可以连接的边数
}
}
for(i = 0; i < mp.size(); i++)
{
for(j = i+1; j<mp.size(); j++)
{
sum +=mp[i]*mp[j];//没有限制的块,两两之间进行连接
}
sum+=mp[i]*maxx;//没有限制的块与点数最多的有限制的块连接
}
printf("%d\n",sum-m);//减去已经连接的m条边
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: