C. Hongcow Builds A Nation 并查集
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 并查集的更多相关文章
- C. Hongcow Builds A Nation
C. Hongcow Builds A Nation time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces 744A. Hongcow Builds A Nation
A. Hongcow Builds A Nation 题意: 现在有 n 个点 ,m 条边组成了一个无向图 , 其中有 k 个特殊点, 这些特殊点之间不能连通 ,问可以再多加几条边? 因为$x^2+y ...
- 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 ...
- Codeforces 745C:Hongcow Builds A Nation(并查集)
http://codeforces.com/problemset/problem/744/A 题意:在一个图里面有n个点m条边,还有k个点是受限制的,即不能从一个受限制的点走到另外一个受限制的点(有路 ...
- Codeforces Round #385 (Div. 2) C - Hongcow Builds A Nation
题目链接:http://codeforces.com/contest/745/problem/C 题意:给出n个点m条边,还有k个不能连通的点,问最多能添加几条边. 要知道如果有n个点最多的边是n*( ...
- 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 ...
- 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 ...
- P3043 [USACO12JAN]牛联盟Bovine Alliance(并查集)
P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
随机推荐
- Cookie防伪造防修改 电商课题:cookie防篡改
主要防止非法用户修改cookie信息,以及cookie的超时时间 传统cookie存储,Cookie(name, value),value很容易就被篡改. 防修改cookie存储,Cookie(nam ...
- WPF的WebBrowser屏蔽弹出脚本错误窗体
WPF自带的WebBrowser在訪问一些有问题的网页时常常跳出非常多提示脚本错误的窗体, 可是WPF没有自带屏蔽这些窗体的方法或属性. 所以网上找来一使用反射的方法来屏蔽弹出脚本错误窗体的方法, 非 ...
- Android之——经常使用手机号码功能
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47374415 有些Android手机中会带有一些经常使用号码的功能,比方订餐电话. ...
- 两个喜欢的"新"C#语法
现在C#比较新的语法,我都十分喜欢. 比如属性可设默认值: public string Name { get; set; } = "张三"; 还有一个就是拼接字符串. 以往,通常都 ...
- Hackrank Kingdom Division 树形DP
题目链接:传送门 题意: 给你一棵树,n个点 每个点可以染成红色和蓝色 但是红色的点与其相邻的点中必须有红色节点,蓝色也是 问你有多少种染色的方案 题解: 树形dp 先转化为有根树,取1为根 设定dp ...
- Deep Learning 27:Batch normalization理解——读论文“Batch normalization: Accelerating deep network training by reducing internal covariate shift ”——ICML 2015
这篇经典论文,甚至可以说是2015年最牛的一篇论文,早就有很多人解读,不需要自己着摸,但是看了论文原文Batch normalization: Accelerating deep network tr ...
- 滚动条样式优化(CSS3自定义滚动条样式 -webkit-scrollbar)
有时候觉得浏览器自带的原始滚动条不是很美观,那webkit浏览器是如何自定义滚动条的呢? Webkit支持拥有overflow属性的区域,列表框,下拉菜单,textarea的滚动条自定义样式.当然,兼 ...
- javaScript复制粘贴
1.clipboard.js 实现了纯 JavaScript (无 Flash)的浏览器内容复制到系统剪贴板的功能.可以在浏览器和 Node 环境中使用.支持 Chrome 42+.Firefox 4 ...
- 什么叫强类型的DATASET
强类型DataSet,是指需要预先定义对应表的各个字段的属性和取值方式的数据集.对于所有这些属性都需要从DataSet, DataTable, DataRow继承,生成相应的用户自定义类.强类型的一个 ...
- validationEngine验证的使用
改校验的方法功能很强大,具体查看api http://code.ciaoca.com/jquery/validation_engine/ 效果: