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

把他们并查集后,

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

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

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <assert.h>
  7. #define IOS ios::sync_with_stdio(false)
  8. using namespace std;
  9. #define inf (0x3f3f3f3f)
  10. typedef long long int LL;
  11.  
  12. #include <iostream>
  13. #include <sstream>
  14. #include <vector>
  15. #include <set>
  16. #include <map>
  17. #include <queue>
  18. #include <string>
  19. const int maxn = 1e4 + ;
  20. int fa[maxn];
  21. int tofind(int x) {
  22. if (fa[x] == x) return x;
  23. else return fa[x] = tofind(fa[x]);
  24. }
  25. void tomerge(int x, int y) {
  26. x = tofind(x);
  27. y = tofind(y);
  28. if (x != y) fa[y] = x;
  29. }
  30. int tohash[maxn];
  31. bool e[maxn][maxn];
  32. vector<int>gg[maxn];
  33. int cc[maxn];
  34. int id[maxn];
  35. bool vis[maxn];
  36. bool has[maxn];
  37. void work() {
  38. int n, m, k;
  39. scanf("%d%d%d", &n, &m, &k);
  40. for (int i = ; i <= maxn - ; ++i) fa[i] = i;
  41. for (int i = ; i <= k; ++i) {
  42. int x;
  43. scanf("%d", &x);
  44. tohash[x] = ;
  45. cc[i] = x;
  46. }
  47. for (int i = ; i <= m; ++i) {
  48. int u, v;
  49. scanf("%d%d", &u, &v);
  50. e[u][v] = e[v][u] = ;
  51. tomerge(u, v);
  52. }
  53. for (int i = ; i <= n; ++i) {
  54. gg[tofind(i)].push_back(i);
  55. }
  56. // for (int i = 1; i <= n; ++i) {
  57. // for (int j = 0; j < gg[tofind(i)].size(); ++j) {
  58. // printf("%d ", gg[tofind(i)][j]);
  59. // }
  60. // printf("\n");
  61. //
  62. // }
  63. int lenid = ;
  64. for (int i = ; i <= n; ++i) {
  65. if (vis[tofind(i)]) continue;
  66. vis[tofind(i)] = true;
  67. // cout << "ff" << endl;
  68. bool flag = false;
  69. for (int j = ; j < gg[tofind(i)].size(); ++j) {
  70. if (tohash[gg[tofind(i)][j]]) {
  71. flag = true;
  72. has[tofind(i)] = true;
  73. break;
  74. }
  75. }
  76. if (!flag) {
  77. id[++lenid] = tofind(i);
  78. }
  79. }
  80. int togo = n + ;
  81. for (int i = ; i <= lenid; ++i) {
  82. tomerge(togo, id[i]);
  83. }
  84. for (int i = ; i <= n; ++i) {
  85. if (has[tofind(i)]) continue;
  86. // cout << "ff" << endl;
  87. assert(tofind(i) == togo);
  88. gg[tofind(i)].push_back(i);
  89. }
  90. int ans = ;
  91. int mx = -inf;
  92. memset(vis, , sizeof vis);
  93. for (int i = ; i <= n; ++i) {
  94. if (!has[tofind(i)]) continue;
  95. // cout << "ff" << endl;
  96. if (vis[tofind(i)]) continue;
  97. vis[tofind(i)] = true;
  98. int tt = gg[tofind(i)].size();
  99. mx = max(mx, tt);
  100. for (int j = ; j < gg[tofind(i)].size(); ++j) {
  101. for (int k = j + ; k < gg[tofind(i)].size(); ++k) {
  102. int u = gg[tofind(i)][j];
  103. int v = gg[tofind(i)][k];
  104. if (!e[u][v]) {
  105. ans++;
  106. // cout << u << " " << v << endl;
  107. }
  108. }
  109. }
  110. }
  111. if (lenid != ) {
  112. // cout << ans << endl;
  113. // cout << "ff" << endl;
  114. for (int i = ; i < gg[togo].size(); ++i) {
  115. for (int j = i + ; j < gg[togo].size(); ++j) {
  116. int u = gg[togo][i];
  117. int v = gg[togo][j];
  118. if (!e[u][v]) ans++;
  119. }
  120. }
  121. }
  122. // cout << ans << endl;
  123. // cout << gg[id[1]].size() << endl;
  124. ans += mx * gg[togo].size();
  125. cout << ans << endl;
  126. }
  127. int main() {
  128. #ifdef local
  129. freopen("data.txt", "r", stdin);
  130. // freopen("data.txt", "w", stdout);
  131. #endif
  132. work();
  133. return ;
  134. }

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. CF 234 C Weather(粗暴方法)

    C. Color Stripe time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  2. Android开发文档翻译之-Services

    Service是一种能长期在后台运行同一时候不须要与用户进行交互的应用组件.其它组件能够开启service,开启后service能够自行运行及时用户已经切换到其它的应用.此外,组件能够与service ...

  3. React通过Ajax获取数据

    React 组件的数据可以通过 componentDidMount 方法中的 Ajax 来获取,当从服务端获取数据库可以将数据存储在 state 中,再用 this.setState 方法重新渲染 U ...

  4. QC ALM 11创建域、项目和用户

    一旦HP-ALM安装,我们仅仅能继续创建域.项目和用户使用后的ALM工作.以下是步骤来创建项目.域和用户.       一.创建域 1.对于创建域,第一步是进入站点管理员页面.开展QC使用URL - ...

  5. C++手稿:std::string

    字符串在非常多编程语言中已经成为基本数据类型,C语言中我们使用char*来手动申请和维护字符串, 在C++中,能够使用std::string来方便地创建和操作字符串. string是一个模板类.它有b ...

  6. 使用JDBC 插入向数据库插入对象

    package com.ctl.util; import java.io.IOException; import java.lang.reflect.Field; import java.lang.r ...

  7. 2016/05/10 thinkphp 3.2.2 ①系统常量信息 ②跨控制器调用 ③连接数据库配置及Model数据模型层 ④数据查询

    [系统常量信息] 获取系统常量信息: 如果加参数true,会分组显示: 显示如下: [跨控制器调用] 一个控制器在执行的时候,可以实例化另外一个控制,并通过对象访问其指定方法. 跨控制器调用可以节省我 ...

  8. java垃圾回收机制的使用

    public class Test { public static void main(String[] args) throws Exception { Book b=new Book(true); ...

  9. HDU4009 Transfer water —— 最小树形图 + 不定根 + 超级点

    题目链接:https://vjudge.net/problem/HDU-4009 Transfer water Time Limit: 5000/3000 MS (Java/Others)    Me ...

  10. IIS application pool access desktop denied

    https://stackoverflow.com/questions/5437723/iis-apppoolidentity-and-file-system-write-access-permiss ...