【26.42%】【codeforces 745C】Hongcow Builds A Nation

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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
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.
【题目链接】:http://codeforces.com/contest/745/problem/C

【题解】

先把联通块全都搞出来;
显然那些不是特殊的联通块全都加在一起然后和特殊联通块中点数最多的合在一起能够形成的边更多.
则加在一起就好;
设这个联通块里面的点的个数为n;
则边的个数为n*(n-1)/2;
最后用总的边数减去m就好;
这里写图片描述
那些没有加非特殊联通块的特殊联通块也可以加边的,不要漏了。

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int MAXN = 1e3+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int n,m,k;
int c[MAXN],f[MAXN],num[MAXN],si[MAXN];

int ff(int x)
{
    if (f[x]!=x)
        f[x] = ff(f[x]);
    else
        return x;
    return f[x];
}

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    rei(n);rei(m);rei(k);
    rep1(i,1,n)
        f[i] = i;
    rep1(i,1,k)
        rei(c[i]);
    rep1(i,1,m)
    {
        int x,y;
        rei(x);rei(y);
        int r1 = ff(x),r2 = ff(y);
        if (r1!=r2)
            f[r1] = r2;
    }
    rep1(i,1,n)
        si[ff(i)]++;
    rep1(i,1,k)
        num[i] = si[ff(c[i])];
    int rest = n;
    rep1(i,1,k)
        rest-=num[i];
    int po = max_element(num+1,num+1+k)-num;
    num[po] += rest;
    int ans = 0;
    rep1(i,1,k)
        ans += num[i]*(num[i]-1)/2;
    cout << ans - m<<endl;
    return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/7626804.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值