C. 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 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 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 c_1, _c_2, …, _c__k (1 ≤ c__i ≤ 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 u__i and v__i (1 ≤ u__i, v__i ≤ n). This denotes an undirected edge between nodes u__i and v__i.
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
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个特殊的点,问最多再能连多少条边,要求k个点是两两不联通的;
思路:并查集:
找出联通块,然后将没有那些特殊点的先合并再一块-->t,sum为答案,sum+=cnt[t]*(cnt[t]-1)/2-原来有的边,然后再在特殊的点中找含点最多的和他和并统计答案,并且加上其他特殊联通块最多能加的边。
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
手机扫一扫
移动阅读更方便
你可能感兴趣的文章