CodeForces - 744A (并查集)


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;
    printf("%d", ans);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值