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. MySQL多实例配置(一)

    MySQL数据库的集中化运维,能够通过在一台MySQL数据库服务器上,部署多个MySQL实例.该功能是通过mysqld_multi来实现.mysqld_multi用于管理多个mysqld的服务进程,这 ...

  2. hdu 3183 A Magic Lamp(给一个n位的数,从中删去m个数字,使得剩下的数字组成的数最小(顺序不能变),然后输出)

    1.题目大意是,给你一个1000位的数,要你删掉m个为,求结果最小数. 思路:在n个位里面删除m个位.也就是找出n-m个位组成最小数 所以在区间 [0, m]里面找最小的数.相应的下标标号i 接着找区 ...

  3. Node 即学即用 笔记 思维导图

    Node即学即用   REPL(Read-Evaluate-Print-Loop)     console.log     .clear .help .exit     require('http') ...

  4. Visual Studio 2012 Fakes框架测试驱动开发TDD教程

    一.前言 最近团队要尝试TDD(测试驱动开发)的实践,很多人习惯了先代码后测试的流程,对于TDD总心存恐惧,认为没有代码的情况下写测试代码时被架空了,没法写下来,其实,根据个人实践经验,TDD并不可怕 ...

  5. 深刻理解Java中形參与实參,引用与对象的关系

    声明:本博客为原创博客,未经同意.不得转载! 原文链接为http://blog.csdn.net/bettarwang/article/details/30989755 我们都知道.在Java中,除了 ...

  6. android WIFI信息获取

    在androi中WIFI信息的获取能够通过系统提供的WIFI Service获取 [java]  WifiManager wifi_service = (WifiManager)getSystemSe ...

  7. PHP使用debug_backtrace方法跟踪代码调用

    在开发过程中,例如要修改别人开发的代码或调试出问题的代码,需要对代码流程一步步去跟踪,找到出问题的地方进行修改.如果有一个方法可以获取到某段代码是被哪个方法调用,并能一直回溯到最开始调用的地方(包括调 ...

  8. Qt 学习之路 2(19):事件的接受与忽略(当重写事件回调函数时,时刻注意是否需要通过调用父类的同名函数来确保原有实现仍能进行!有好几个例子。为什么要这么做?而不是自己去手动调用这两个函数呢?因为我们无法确认父类中的这个处理函数有没有额外的操作)

    版本: 2012-09-29 2013-04-23 更新有关accept()和ignore()函数的相关内容. 2013-12-02 增加有关accept()和ignore()函数的示例. 上一章我们 ...

  9. 8088汇编跳转和PSW状态字寄存器

    DDD 8088 汇编跳转 日期:2003年6月12日  出处:嬴政天下整理收藏  作者:看雪  人气: 8735 8088 汇编跳转 一.状态寄存器 PSW(Program Flag)程序状态字寄存 ...

  10. vim插件:显示树形目录插件NERDTree安装 和 使用【转】

    本文转载自:https://my.oschina.net/VASKS/blog/388907 下载和配置 NERDTree插件的官方地址如下,可以从这里获取最新的版本 https://github.c ...