Codeforces 744A 容斥&并查集

本文探讨了一个有趣的图论问题,Hongcow作为世界统治者,希望通过添加尽可能多的边来改善各国国内的交通状况,同时保持国家间的独立性。文章分析了如何在保证图稳定性的前提下,最大化新增边的数量,并提供了详细的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

A. Hongcow Builds A Nation

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

Simple Input

4 1 2
1 3
1 2

Simple Output
2

题目大意:有n个城市,m个边,K个国家。每个国家的首都之间没有通路,问最多能在原图中添加多少路径。

分析:K个国家之间没有任何路径相连,即有K个无并集的集合。要使得可添加的路径最多,即只要在K个点集合的基础上做出一个完全图,就能使得路径数量最多。所求结果,添加的路径数量就是最多的路径减去原有的路径。

则,建立一个并查集,每次输入都将集合更新。那么最后就有3部分,一是城市最多的国家,二是其他的国家,三是没有与任何首都相连的国家。推理可得,把独立城市与最大的国家相连,如此得到的无向完全图边数最多。

Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <string>
#include <cstring>

using namespace std;

const int maxn = 1e5+10;
typedef long long ll;
int num[maxn];
int F[1010];
int fnd(int t){
    if(F[t] == t)   return t;
    return F[t] = fnd(F[t]);
}
void join(int a,int b){
    int t1 = fnd(a);
    int t2 = fnd(b);
    if(t1!=t2) F[t1] = t2;
}
int main()
{
    int n,m,k;
    int cap[maxn];
    cin >> n >> m >> k;

    memset(num,0,sizeof(num));
    memset(F,0,sizeof(F));
    for(int i=1; i<=n; ++i)
        F[i] = i;       //initialize F
    for(int i=1; i<=k ;++i)
        cin >> cap[i];
    for(int i=1,a,b; i<=m; ++i){
        cin >> a >> b;
        join(a,b);
    }

    for(int i=1; i<=n; ++i)
        num[fnd(i)]++;
    int maxn = 0,lef = n;
    int ans = 0;
    for(int i=1; i<=k; ++i){
        num[cap[i]] = num[fnd(cap[i])];
        maxn = max(maxn,num[cap[i]]);
        lef -= num[cap[i]];
        ans += (num[cap[i]] - 1) * (num[cap[i]])/2;
    }                
    ans += (lef+maxn) * (lef+maxn-1)/2;
    ans -= maxn*(maxn-1)/2 + m;
    cout << ans ;
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值