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. Linux下获取线程TID的方法

    如何获取进程的PID(process ID)? 可以使用: #include <unistd.h> pid_t getpid(void); 通过查看头文件说明,可以得到更详细的信息: fi ...

  2. javaEE之------ApectJ的切面技术===标签

    如今比較流行了aop技术之中的一个========标签 实现步骤: 一,导入aop标签 方法,打开aop包.里面就有. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5 ...

  3. Centos 6.4 搭建LANMP一键安装版

    lanmp一键安装包是wdlinux官网2010年开始推出的lamp,lnmp,lnamp(apache,nginx,php,mysql,zend,eAccelerator,pureftpd)应用环境 ...

  4. debian包之间的关系

    1 debian包之间存在两大类关系 第一,依赖 第二,冲突 2 依赖类关系 2.1 depends 2.2 pre-depends 2.3 recommends 2.4 suggests 2.5 e ...

  5. 城域网IPv6过渡技术—NAT64+DNS64 Test for IPv6 DNS64/NAT64 Compatibility Regularly

    城域网IPv6过渡技术—NAT64+DNS64 - 51CTO.COM http://network.51cto.com/art/201311/419623.htm Supporting IPv6 D ...

  6. 动态追踪技术 Dynamic Tracing

    https://openresty.org/posts/dynamic-tracing/ 工欲性能调优,必先利其器(2)- 火焰图| PingCAP https://pingcap.com/blog- ...

  7. iOS设备,fixed布局出问题

    window.deviceId = '{{$deviceId}}'; window.iOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? t ...

  8. echo 到 stderr

    This question is old, but you could do this, which facilitates reading: >&2 echo "error& ...

  9. robotframework执行用例时,报错selenium.common.exceptions.WebDriverException: Message: unknown error: cannot get automation extension from unknown error: page could not be found: chrome-extension://aapnijgdinl

    在用robotframework编写移动端测试用例(用chrome浏览器模拟手机浏览器),执行用例时, 报错selenium.common.exceptions.WebDriverException: ...

  10. 小程序-demo:template

    ylbtech-小程序-demo: 1.返回顶部 1.app.js 2.app.json 3.app.wxss 4.project.config.json 5.pages 6.images 7. 2. ...