Codeforces 745C - Hongcow Builds A Nation 并查集乱搞,注意路径压缩

原题:http://codeforces.com/problemset/problem/745/C

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 kcountries 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.

也算有所收获吧。。首先关于并查集,我原先比赛时写这题一直mle,以为是数组开大了然而改到合理范围也是mle。原来是并查集用find的时候爆栈了,因为合并的时候没有进行路径压缩!!!所以我以前写的很多和并查集有关的代码是错的!!如果数据强的话就过不了的

一定记得

pre[find(b)]=find(a)

题意:国王想要国家内的n个地点尽可能地连起来,就是n个点尽可能多的边啦(n*(n-1)/2)。但是有个限制就是,如果该节点是地方政治节点的话,它是不能和别的地方政治节点连起来的。题会给n个节点,k个政治节点,以及已经修好的m条边,求最多还能修多少条路

我看到好多大爷交的代码都是dfs。。。我靠这也能搜索啊。。。。

我比赛的时候就瞎搞的,先统计最多多少条边,用并查集维护的话,接下来就是数学的事情了。。。

然而比赛的时候脑子不清楚。。。。过程变量名写错了啥的,。。。。

话说我的莫名其妙变量取名习惯确实该改改了。。。赛后看自己代码简直是个啥玩意儿啊咋回事啊

#include <string.h>
#include <stdio.h>
#include <math.h>
//#include <iostream>
//using namespace std;
const int maxn = 1010;
bool vis[maxn];
bool cou[maxn];
int killme[maxn];
int pre[maxn];

int find(int x){
    return pre[x]==x?x:pre[x]=find(pre[x]);
}
void join(int a,int b){
    //pre[b]=a; 居然是这里让我MLE,路经压缩很重要啊!!
    pre[find(b)]=find(a);
}
int main(){
    int n,m,i,j,k,a,b;
    memset(vis, false, sizeof(vis));
    memset(killme, 0, sizeof(killme));
    scanf("%d%d%d",&n,&m,&k);
    for(i=1;i<=n;i++){
        pre[i] = i;
    }
    for(i=1;i<=k;i++){
        scanf("%d",&a);
        vis[a] = true;
    }
    for(i=1;i<=m;i++){
        scanf("%d%d",&a,&b);
        join(a, b);
    }
    int ans = n*(n-1)/2;
    memset(cou, false, sizeof(cou));
    for(i=1;i<=n;i++){
        find(i);
    }
    for(i=1;i<=n;i++){
        if(vis[i]){
            cou[pre[i]] = true;
        }
        killme[pre[i]]++;
    }
    int teki = 0;
    for(i=1;i<=n;i++){
        if(cou[i]){
            teki += killme[i];
        }
    }
    int now = teki;
    for(i=1;i<=n;i++){
        if(cou[i]){
            ans -= killme[i]*(now-killme[i]);
            now -= killme[i];
        }
    }
    int connect;
    killme[connect] = 0;
    for(i=1;i<=n;i++){
        int bojack = pre[i];
        if(!cou[bojack]){
            now = 0;
            connect = 0;
            for(j=1;j<=n;j++){
                if(cou[j]){
                    if(killme[j]>now){
                        connect = j;
                        now = killme[connect];
                    }
                }
            }
            ans -= (killme[bojack]*(teki-killme[connect]));
            killme[connect] += killme[bojack];
            cou[bojack] = true;
            teki += killme[bojack];
        }
    }
    printf("%d\n",ans-m);
    return 0;
}







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值