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 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
随机推荐
- CF 234 C Weather(粗暴方法)
C. Color Stripe time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Android开发文档翻译之-Services
Service是一种能长期在后台运行同一时候不须要与用户进行交互的应用组件.其它组件能够开启service,开启后service能够自行运行及时用户已经切换到其它的应用.此外,组件能够与service ...
- React通过Ajax获取数据
React 组件的数据可以通过 componentDidMount 方法中的 Ajax 来获取,当从服务端获取数据库可以将数据存储在 state 中,再用 this.setState 方法重新渲染 U ...
- QC ALM 11创建域、项目和用户
一旦HP-ALM安装,我们仅仅能继续创建域.项目和用户使用后的ALM工作.以下是步骤来创建项目.域和用户. 一.创建域 1.对于创建域,第一步是进入站点管理员页面.开展QC使用URL - ...
- C++手稿:std::string
字符串在非常多编程语言中已经成为基本数据类型,C语言中我们使用char*来手动申请和维护字符串, 在C++中,能够使用std::string来方便地创建和操作字符串. string是一个模板类.它有b ...
- 使用JDBC 插入向数据库插入对象
package com.ctl.util; import java.io.IOException; import java.lang.reflect.Field; import java.lang.r ...
- 2016/05/10 thinkphp 3.2.2 ①系统常量信息 ②跨控制器调用 ③连接数据库配置及Model数据模型层 ④数据查询
[系统常量信息] 获取系统常量信息: 如果加参数true,会分组显示: 显示如下: [跨控制器调用] 一个控制器在执行的时候,可以实例化另外一个控制,并通过对象访问其指定方法. 跨控制器调用可以节省我 ...
- java垃圾回收机制的使用
public class Test { public static void main(String[] args) throws Exception { Book b=new Book(true); ...
- HDU4009 Transfer water —— 最小树形图 + 不定根 + 超级点
题目链接:https://vjudge.net/problem/HDU-4009 Transfer water Time Limit: 5000/3000 MS (Java/Others) Me ...
- IIS application pool access desktop denied
https://stackoverflow.com/questions/5437723/iis-apppoolidentity-and-file-system-write-access-permiss ...