Codeforces 745C

编程入门 行业动态 更新时间:2024-10-23 23:20:25

<a href=https://www.elefans.com/category/jswz/34/1770097.html style=Codeforces 745C"/>

Codeforces 745C

原题:
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 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.

也算有所收获吧。。首先关于并查集,我原先比赛时写这题一直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;
}







更多推荐

Codeforces 745C

本文发布于:2024-02-27 14:29:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1706899.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Codeforces

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!