http://codeforces.com/contest/745/problem/C

把他们并查集后,

其他没有连去government的点,全部放去同一个并查集,然后选择一个节点数最多的government集合,连接过去即可。

至于有多少条边增加,可以暴力判断。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e4 + ;
int fa[maxn];
int tofind(int x) {
if (fa[x] == x) return x;
else return fa[x] = tofind(fa[x]);
}
void tomerge(int x, int y) {
x = tofind(x);
y = tofind(y);
if (x != y) fa[y] = x;
}
int tohash[maxn];
bool e[maxn][maxn];
vector<int>gg[maxn];
int cc[maxn];
int id[maxn];
bool vis[maxn];
bool has[maxn];
void work() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for (int i = ; i <= maxn - ; ++i) fa[i] = i;
for (int i = ; i <= k; ++i) {
int x;
scanf("%d", &x);
tohash[x] = ;
cc[i] = x;
}
for (int i = ; i <= m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
e[u][v] = e[v][u] = ;
tomerge(u, v);
}
for (int i = ; i <= n; ++i) {
gg[tofind(i)].push_back(i);
}
// for (int i = 1; i <= n; ++i) {
// for (int j = 0; j < gg[tofind(i)].size(); ++j) {
// printf("%d ", gg[tofind(i)][j]);
// }
// printf("\n");
//
// }
int lenid = ;
for (int i = ; i <= n; ++i) {
if (vis[tofind(i)]) continue;
vis[tofind(i)] = true;
// cout << "ff" << endl;
bool flag = false;
for (int j = ; j < gg[tofind(i)].size(); ++j) {
if (tohash[gg[tofind(i)][j]]) {
flag = true;
has[tofind(i)] = true;
break;
}
}
if (!flag) {
id[++lenid] = tofind(i);
}
}
int togo = n + ;
for (int i = ; i <= lenid; ++i) {
tomerge(togo, id[i]);
}
for (int i = ; i <= n; ++i) {
if (has[tofind(i)]) continue;
// cout << "ff" << endl;
assert(tofind(i) == togo);
gg[tofind(i)].push_back(i);
}
int ans = ;
int mx = -inf;
memset(vis, , sizeof vis);
for (int i = ; i <= n; ++i) {
if (!has[tofind(i)]) continue;
// cout << "ff" << endl;
if (vis[tofind(i)]) continue;
vis[tofind(i)] = true;
int tt = gg[tofind(i)].size();
mx = max(mx, tt);
for (int j = ; j < gg[tofind(i)].size(); ++j) {
for (int k = j + ; k < gg[tofind(i)].size(); ++k) {
int u = gg[tofind(i)][j];
int v = gg[tofind(i)][k];
if (!e[u][v]) {
ans++;
// cout << u << " " << v << endl;
}
}
}
}
if (lenid != ) {
// cout << ans << endl;
// cout << "ff" << endl;
for (int i = ; i < gg[togo].size(); ++i) {
for (int j = i + ; j < gg[togo].size(); ++j) {
int u = gg[togo][i];
int v = gg[togo][j];
if (!e[u][v]) ans++;
}
}
}
// cout << ans << endl;
// cout << gg[id[1]].size() << endl;
ans += mx * gg[togo].size();
cout << ans << endl;
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

C. Hongcow Builds A Nation 并查集的更多相关文章

  1. C. Hongcow Builds A Nation

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

  2. Codeforces 744A. Hongcow Builds A Nation

    A. Hongcow Builds A Nation 题意: 现在有 n 个点 ,m 条边组成了一个无向图 , 其中有 k 个特殊点, 这些特殊点之间不能连通 ,问可以再多加几条边? 因为$x^2+y ...

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

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

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

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

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

  6. Codeforces Round #385 (Div. 2) A,B,C 暴力,模拟,并查集

    A. Hongcow Learns the Cyclic Shift time limit per test 2 seconds memory limit per test 256 megabytes ...

  7. Codeforces Round #385 (Div. 2)A B C 模拟 水 并查集

    A. Hongcow Learns the Cyclic Shift time limit per test 2 seconds memory limit per test 256 megabytes ...

  8. P3043 [USACO12JAN]牛联盟Bovine Alliance(并查集)

    P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...

  9. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

随机推荐

  1. yii2利用自带UploadedFile上传图片

    创建一个 models/UploadForm.php: <?php namespace app\models; use yii\base\Model; use yii\web\UploadedF ...

  2. SQL2012 尝试读取或写入受保护的内存。这通常指示其它内存已损坏

    今天打开SQL2012,突然就连接不了数据库.一開始还以为是某个server崩溃了.结果试了好几个.都还是如此,弹出提演示样例如以下: 尝试读取或写入受保护的内存.这通常仅仅是其它内存已损坏.(Sys ...

  3. 鸡肋的JdbcRDD

          今天准备将mysql的数据倒腾到RDD.非常早曾经就知道有一个JdbcRDD.就想着使用一下,结果发现却是鸡肋一个.       首先,看看JdbcRDD的定义: * An RDD tha ...

  4. 在类的头文件里尽量少引入其它头文件 &lt;&lt;Effective Objective-C&gt;&gt;

    与C 和C++ 一样,Objective-C 也使用"头文件"(header file) 与"实现文件"(implementation file)来区隔代码.用 ...

  5. PHP中常见的header类型

    <?php // 使用 mime_content_type() 查看 $mimetypes=array( 'ez' => 'application/andrew-inset', 'hqx' ...

  6. 一个程序员对微信小程序的看法

      我们公司用两周的时间开发了一款微信小程序,叫<如e支付>,大家可以去体验一下.由于接口都是写好的,所以开发起来很快.我将从4个不同的角度来介绍我对微信小程序的理解. 1.技术的角度   ...

  7. MongoDB数据库的初识

    1,MongoDB是基于分布式文件存储的数据库,有c++语言编写,旨在为WEB应用提供可扩展的高效性能数据存储解决方案. MongoDB是一个介于关系型数据库和非关系数据库之间的产品,是非关系数据库当 ...

  8. struts2 Action获取表单数据

    1.通过属性驱动式 1.首先设置 表单中的数据的name值 如:<input type="text" name="username" value=&quo ...

  9. myeclipse提示:Syntax error on tokens, delete these tokens怎么解决

    有中文字符或者符号,包括空格. 上次遇到一个问题,检查了一遍语法没错误, 后来发现是拷贝代码的时候有一部分中文空格没删除,就出现这个问题了. 一个个删除就OK了.

  10. CAS和ABA

    1 CAS compare and swap的缩写,详见乐观锁和悲观锁. 2 ABA 就是说,我获取的旧值是A,然后被人修改成了B,但是又被人修改成了A,我就认为并没有修改,更新内存. 解决办法,给每 ...