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 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.
Example 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連通塊的最多邊數為(n*n-1)/2,做法是先建立起并查集,處理出所有非政府連通塊的點的數量x和以及每一個包含政府的連通塊,然后把x加入點集最大的包含政府連通塊中,然后對每一個包含政府連通塊求答案,最終減去已有邊數。
#include<iostream>#include<stdio.h>#include<algorithm>#include<vector>using namespace std;int n, m, k;bool gov[1005];int f[1005];int total[1005];vector<int> v;int F(int x){ return f[x] == x ? x : (f[x] = F(f[x]));}int main(){ scanf("%d%d%d", &n, &m, &k); int num, a, b; for (int i = 0; i < k; i++){ scanf("%d", &num); gov[num] = 1; } for (int i = 1; i <= n; i++){ f[i] = i; total[i] = 1; } for (int i = 0; i < m; i++){ scanf("%d%d", &a, &b); if (gov[F(a)]){ f[F(b)] = F(a); } else {f[F(a)] = F(b); } } int nogovnum = 0; for (int i = 1; i <= n; i++){ if (F(i) != i){ total[F(i)]++; } } for (int i = 1; i <= n; i++){ if (f[i] == i){ if (gov[i])v.push_back(total[i]); else nogovnum += total[i]; } } int ans = 0; sort(v.begin(), v.end()); int len = v.size(); v[len - 1] += nogovnum; for (int i = 0; i < len; i++){ int QQ = v[i]; ans += (qq*(qq - 1)) / 2; } ans -= m;新聞熱點
疑難解答