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

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

CodeForces - 744A (并查集)

2019-11-08 18:23:29
字體:
來源:轉載
供稿:網友

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;
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鸡西市| 东光县| 石阡县| 武安市| 兴宁市| 鄂伦春自治旗| 文化| 汝阳县| 娄烦县| 分宜县| 新龙县| 普兰店市| 常熟市| 崇信县| 聂拉木县| 巴林右旗| 漠河县| 蚌埠市| 尤溪县| 丹江口市| 沙洋县| 顺昌县| 岳阳市| 桃园县| 泸溪县| 常宁市| 怀集县| 宣威市| 耿马| 桓仁| 稻城县| 湖北省| 武清区| 沁源县| 西贡区| 宜城市| 察雅县| 安溪县| 德州市| 台前县| 金阳县|