Codeforces Round #385 (Div. 2)C. Hongcow Builds A Nation【并查集+贪心】好题~

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 nm and k (1 ≤ n ≤ 1 0000 ≤ m ≤ 100 0001 ≤ 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.

题目大意:

给你一个无向图,其中有N个点,M条无向边,已知有K个重要点,其中任意两个重要点如果都不能直接或者间接连接(就是任意两个重要点不连通),那么就称作这个图当前是稳定的。

初始的图一定是稳定的,问我们还最多可以添加多少条边,能够使得图还是稳定的。


思路:


1、首先我们使用并查集将所有联通块都处理出来。


2、因为我们每个联通块中最多只有一个重要点,那么我们肯定一个联通块我们可以对其进行添加边至:tmpn*(tmpn-1)【这里tmpn表示这个联通块中点的个数,即我们将联通块变成一个五向完全图】;那么我们对于没有重要点的联通块,我们可以将其和有重要点的联通块连接在一起,然而我们对应要选择一个有重要点的联通块连接在一起,又因为我们连接在一起之后肯定是将其变成无向完全分图的,那么我们肯定是要找包含重要点的点数最多的联通块来与其合并,那么我们:

①我们使用并查集将所有联通块都处理出来

②找到具有点数最多的包含重要点的联通块,记录点数maxn.

③对于其他包含重要点的联通块,肯定边数需要增加至tmpn*(tmpn-1);

④对于不包含重要点的联通块,将其与②中的联通块连接在一起,这部分的点数和我们记做cnt.

⑤那么ans=(cnt+maxn)*(cnt+maxn-1)/2+Σ其他各个具有重要点的联通块的点数tmp*(tmp-1)/2;


Ac代码:


#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int mmm[1005];
int f[1005];
int flag[1005];
int vis[1005];
int sum[1005];
int bian[1005];
int n,m,k;
int find(int a)
{
    int r=a;
    while(f[r]!=r)
        r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    bian[A]++;
    if(A!=B)
    {
        f[B]=A;
        sum[A]+=sum[B];
        flag[A]+=flag[B];
    }
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        memset(mmm,0,sizeof(mmm));
        memset(vis,0,sizeof(vis));
        memset(flag,0,sizeof(flag));
        memset(sum,0,sizeof(sum));
        for(int i=0;i<k;i++)
        {
            int x;
            scanf("%d",&x);
            mmm[x]=1;
            flag[x]=1;
        }
        for(int i=1;i<=n;i++)
        {
            f[i]=i;
            sum[i]=1;
        }
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            merge(x,y);
        }
        int output=0;
        int maxn=0;
        for(int i=1;i<=n;i++)
        {
            if(f[i]==i&&flag[i]==1)
            {
                maxn=max(maxn,sum[i]);
            }
        }
        int cnt=0;
        int tmp=0;
        for(int i=1;i<=n;i++)
        {
            if(f[i]==i)
            {
                if(flag[i]==1)
                {
                    if(sum[i]==maxn)
                    {
                        if(tmp==0)
                        {
                            tmp=1;
                        }
                        else
                        {
                            output+=sum[i]*(sum[i]-1)/2;
                        }
                    }
                    else
                    {
                        output+=sum[i]*(sum[i]-1)/2;
                    }
                }
                else
                {
                    cnt+=sum[i];
                }
            }
        }
        output+=(cnt+maxn)*(cnt+maxn-1)/2;
        printf("%d\n",output-m);
    }
}









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值