A. Hongcow Builds A Nation

题意: 现在有 n 个点 ,m 条边组成了一个无向图 , 其中有 k 个特殊点, 这些特殊点之间不能连通 ,问可以再多加几条边?

因为$x^2+y^2<=(x+y)^2$,所以找出所有连通块,枚举一个特殊连通块,与其他非特殊连通块合在一起。一个连通块贡献答案:$\frac{Size^2-Size}{2}-已有边$

#include< cstdio >

typedef long long ll;
template
inline void read(T&x)
{
x=0;bool f=0;char c=getchar();
while((c<'0'||c>'9')&&c!='-') c=getchar();if(c=='-')f=1, c=getchar();
while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
x=f?-x:x;
}
const int MAXN(1010);
int n,m,k,a,b,f[MAXN],c[MAXN],size[MAXN],have[MAXN],que[MAXN],tl,Ans,tmp,now;
bool bf[MAXN];
int gf(int x){return x==f[x]?x:f[x]=gf(f[x]);}
int max(int a,int b){return a>b?a:b;}
void Union(int a,int b)
{
int fa=gf(a),fb=gf(b);
if(fa!=fb)
{
size[fa]+=size[fb];
have[fa]+=have[fb];
f[fb]=fa;
}
have[fa]++;
}
int Edge(int x)
{
return (size[x]*(size[x]-1))/2-have[x];
}
int main()
{
// freopen("C.in","r",stdin);
// freopen("C.out","w",stdout);
read(n);read(m);read(k);
for(int i=1;i<=n;i++)f[i]=i,size[i]=1;
for(int i=1;i<=k;i++)read(c[i]);
for(int i=1;i<=m;i++)
{
read(a);read(b); Union(a,b);
}
for(int i=1;i<=n;i++)
if(!bf[gf(i)])que[++tl]=gf(i),bf[gf(i)]=1;
for(int i=1;i<=k;i++)
{
bf[gf(c[i])]=0;
Ans+=Edge(gf(c[i]));
}
// fprintf(stderr,"%d\n",tl);
for(int i=1;i<=tl;i++)
if(bf[que[i]])
{
size[0]+=size[que[i]];
have[0]+=have[que[i]];
}
// fprintf(stderr,"Ans%d\n",Ans);
// fprintf(stderr,"0:%d\n",Edge(0));
now=Ans;
for(int i=1;i<=k;i++)
{
tmp=now;
size[0]+=size[gf(c[i])];
have[0]+=have[gf(c[i])];
tmp-=Edge(gf(c[i]));
tmp+=Edge(0);
// fprintf(stderr,"s0:%d\n",size[0]);
// fprintf(stderr,"h0:%d\n",have[0]);
// fprintf(stderr,"e0:%d\n",Edge(0));
// fprintf(stderr,"tmp:%d\n",tmp);
size[0]-=size[gf(c[i])];
have[0]-=have[gf(c[i])];
Ans=max(Ans,tmp);
}
printf("%d\n",Ans);
return 0;
}

Codeforces 744A. Hongcow Builds A Nation的更多相关文章

  1. Codeforces Round #385 (Div. 2) Hongcow Builds A Nation —— 图论计数

    题目链接:http://codeforces.com/contest/745/problem/C C. Hongcow Builds A Nation time limit per test 2 se ...

  2. C. Hongcow Builds A Nation

    C. Hongcow Builds A Nation time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  3. Codeforces 745C:Hongcow Builds A Nation(并查集)

    http://codeforces.com/problemset/problem/744/A 题意:在一个图里面有n个点m条边,还有k个点是受限制的,即不能从一个受限制的点走到另外一个受限制的点(有路 ...

  4. Codeforces Round #385 (Div. 2) C - Hongcow Builds A Nation

    题目链接:http://codeforces.com/contest/745/problem/C 题意:给出n个点m条边,还有k个不能连通的点,问最多能添加几条边. 要知道如果有n个点最多的边是n*( ...

  5. C. Hongcow Builds A Nation 并查集

    http://codeforces.com/contest/745/problem/C 把他们并查集后, 其他没有连去government的点,全部放去同一个并查集,然后选择一个节点数最多的gover ...

  6. codeforces 744C Hongcow Buys a Deck of Cards

    C. Hongcow Buys a Deck of Cards time limit per test 2 seconds memory limit per test 256 megabytes in ...

  7. Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)

    Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ...

  8. Codeforces.744B.Hongcow's Game(交互 按位统计)

    题目链接 \(Description\) 一个\(n\times n\)的非负整数矩阵\(A\),保证\(A_{i,i}=0\).现在你要对每个\(i\)求\(\min_{j\neq i}A_{i,j ...

  9. Codeforces 744C. Hongcow Buys a Deck of Cards(状压DP)

    这题的难点在于状态的设计 首先显然是个状压,需要一维表示卡的状态,另一维如果设计成天数,难以知道当前的钱数,没法确定是否能够购买新的卡,如果设计成钱数,会发现状态数过多,空间与时间都无法承受.但是可以 ...

随机推荐

  1. Python pandas检查数据中是否有NaN的几种方法

    Python pandas: check if any value is NaN in DataFrame # 查看每一列是否有NaN: df.isnull().any(axis=0) # 查看每一行 ...

  2. OpenStack基础知识-virtualenv工具详解

    1.virtualenv介绍 virtualenv通过创建一个单独的虚拟化python运行环境,将我们所需的依赖安装进去,不同项目之间相互不干扰,从而解决不同的项目之间依赖不同,造成的冲突问题 2.安 ...

  3. yarn快速使用及实践建议

    什么是 yarn? 简单来说,yarn 是一个与 npm 功能相同的工具,用于前端项目的依赖管理.在使用 npm 的项目中,使用 npm 命令的地方都可以使用 yran 来代替. 为什么要使用 yar ...

  4. JavaScript -- 数据存储

    Cookie Web应用程序是使用HTTP协议传输数据的.HTTP协议是无状态的协议. 一旦数据交换完毕,客户端与服务器端的连接就会关闭,再次交换数据需要建立新的连接.这就意味着服务器无法从连接上跟踪 ...

  5. webpack@3.6.0(3)-- 优化

    本篇内容 babel配置 打包调试 第三方资源引入 静态资源的集中输出 babel配置 cnpm i -D babel-core babel-loader babel-preset-es2015 // ...

  6. Codeforces570C 【简单标记】

    题意: 给定一个长为n的字符串(包含小写字母和'.'),有m次操作 每次操作可以修改字符,并询问修改后有多少对相邻的'.' 思路: 标记. #include<bits/stdc++.h> ...

  7. 一个MySQL中两表联合update的例子(并带有group by分组)

    内容简介 本文主要展示了在MySQL中,使用两表联合的方式来更新其中一个表字段值的SQL语句. 也就是update table1 join table2 on table1.col_name1=tab ...

  8. Spring MVC 基于URL的映射规则(注解版)

    好几天没有跟进Spring MVC的学习了,之前看了点源码都忘的差不多了.这次就跟着之前的问题,继续总结下Spring MVC中的小知识. 关于SpringMVC的小demo可以参考这里! url-p ...

  9. Codeforces Round #501 (Div. 3) 1015D Walking Between Houses

    D. Walking Between Houses time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  10. python绘制世界人口地图

    最近看了<python编程:从入门到实践>,里边设计的项目拿来学习学习,绘制世界人口地图. 首先,下载数据,http://data.okfn.org/ ,从这里下载population_d ...