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. SQL中的union,except,intersect用法

    限制:所有查询中的列数和列的数序必须相同 union all:完全整合两个结果集查出所有数据 union:查出两个表的数据并且去除重复的数据 except:去重之后只会保留第一个表中的数据,查询a表在 ...

  2. 【mybatis在控制台打印sql语句配置】

    导入jar包:http://pan.baidu.com/s/1sl8RU37 <strong>############################################### ...

  3. 2018CCPC网络赛A(优先队列,思维)

    #include<bits/stdc++.h>using namespace std;priority_queue<pair<int,int>>q;int main ...

  4. python numpy 判断ndarray 中是否有 nan

    numpy.isnan(myarray).any() 下面讨论了哪一种方法的速度最快 reference: stackoverflow.com/questions/911871/detect-if-a ...

  5. Zjoi2011 看电影

    最近在学习一些概率的东西.. 一个随机试验称为 Laplace 试验,当且仅当它满足如下两个条件: (ⅰ) 试验结果 (样本点) 的个数是有限的.(Ω 是有限集) (ⅱ) 任意两个基本事件的概率均相等 ...

  6. hdu 1729 Stone Game

    Stone Game HDU - 1729 题意: 给定n个箱子,每个箱子的容量为si,每个箱子里最初有ci个石子,每次放入石子不能超过放入前的石子数的平方,谁无法继续放入石子就算输.   /* 这是 ...

  7. 洛谷P4762 [CERC2014]Virus synthesis(回文自动机+dp)

    传送门 回文自动机的好题啊 先建一个回文自动机,然后记$dp[i]$表示转移到$i$节点代表的回文串的最少的需要次数 首先肯定2操作越多越好,经过2操作之后的串必定是一个回文串,所以最后的答案肯定是由 ...

  8. springMVC容器加载源码分析

    springmvc是一个基于servlet容器的轻量灵活的mvc框架,在它整个请求过程中,为了能够灵活定制各种需求,所以提供了一系列的组件完成整个请求的映射,响应等等处理.这里我们来分析下spring ...

  9. windows cmd命令 mkdir生成多个文件bug问题

    [问题现象] 有这样一个bat脚本,目的是为了根据时间创建文件夹 执行后却发现生产的文件夹有两个,名字被分开了,很是纳闷,一度以为自己哪里写错了 [问题原因] 经过查阅资料,一点一点的定位.发现是因为 ...

  10. 1、kubernetes系统基础190622

    1.容器编排工具 docker编排工具三剑客:docker compose, docker swarm, docker machine IDC的操作系统:mesos 提供容器编排框架:marathon ...