题目链接:http://codeforces.com/contest/745/problem/C

C. Hongcow Builds A Nation
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Hongcow is ruler of the world. As ruler of the world, he wants to make it easier for people to travel by road within their own countries.

The world can be modeled as an undirected graph with n nodes and m edges. k of the nodes are home to the governments of the kcountries that make up the world.

There is at most one edge connecting any two nodes and no edge connects a node to itself. Furthermore, for any two nodes corresponding to governments, there is no path between those two nodes. Any graph that satisfies all of these conditions is stable.

Hongcow wants to add as many edges as possible to the graph while keeping it stable. Determine the maximum number of edges Hongcow can add.

Input

The first line of input will contain three integers nm and k (1 ≤ n ≤ 1 000, 0 ≤ m ≤ 100 000, 1 ≤ k ≤ n) — the number of vertices and edges in the graph, and the number of vertices that are homes of the government.

The next line of input will contain k integers c1, c2, ..., ck (1 ≤ ci ≤ n). These integers will be pairwise distinct and denote the nodes that are home to the governments in this world.

The following m lines of input will contain two integers ui and vi (1 ≤ ui, vi ≤ n). This denotes an undirected edge between nodes ui and vi.

It is guaranteed that the graph described by the input is stable.

Output

Output a single integer, the maximum number of edges Hongcow can add to the graph while keeping it stable.

Examples
input
4 1 2
1 3
1 2
output
2
input
3 3 1
2
1 2
1 3
2 3
output
0
Note

For the first sample test, the graph looks like this:

Vertices 1 and 3 are special. The optimal solution is to connect vertex 4 to vertices 1 and 2. This adds a total of 2 edges. We cannot add any more edges, since vertices 1 and 3 cannot have any path between them.

For the second sample test, the graph looks like this:

We cannot add any more edges to this graph. Note that we are not allowed to add self-loops, and the graph must be simple.

题意:

给出一张无向图,图中有k个点为特殊点,且图满足:每对特殊点直接没有通路。问:最多能添加多少条边,使得图仍能满足上述条件?

题解:

1.将每个连通块缩成一个集合,这个集合需要记录的信息有:点的个数,以及是否含有特殊点(最多有1个)。

2.根据集合中点的个数,将集合降序排序。

3.首先计算出一个集合内的所有边(完全图),即:num*(num-1)/2;然后挑选点数最大的两个集合,如果这两个集合最多只有一个特殊点,那么意味着他们可以合并,于是合并,共添加了num1*num2条边。

4.由于步骤3计算的是添加边后,总的边数,所以减去初始图的边数,才为添加的边数。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e6+;
const int MAXN = 1e4+; struct Node
{
bool hav;
int num;
bool operator<(const Node &x)const{
return num>x.num;
}
}q[MAXN]; vector<int>g[MAXN];
bool isgov[MAXN], vis[MAXN]; void dfs(int u, int index)
{
vis[u] = true;
q[index].num++;
if(isgov[u]) q[index].hav = true;
for(int i = ; i<g[u].size(); i++)
if(!vis[g[u][i]])
dfs(g[u][i], index);
} int main()
{
int n, m, k;
scanf("%d%d%d", &n,&m,&k);
memset(isgov, false, sizeof(isgov));
for(int i = ; i<=n; i++) g[i].clear();
for(int i = ; i<=k; i++)
{
int u;
scanf("%d", &u);
isgov[u] = true;
}
for(int i = ; i<=m; i++)
{
int u, v;
scanf("%d%d", &u,&v);
g[u].push_back(v);
g[v].push_back(u);
} int index = ;
memset(q, ,sizeof(q));
memset(vis, false, sizeof(vis));
for(int i = ; i<=n; i++)
if(!vis[i])
dfs(i, ++index); sort(q+,q++index);
int ans = (q[].num-)*q[].num/;
for(int i = ; i<=index; i++)
{
ans += (q[i].num-)*q[i].num/;
if(!q[].hav || !q[i].hav)
{
ans += q[].num*q[i].num;
q[].num += q[i].num;
q[].hav = q[].hav||q[i].hav;
}
} ans -= m;
printf("%d\n", ans);
}

Codeforces Round #385 (Div. 2) Hongcow Builds A Nation —— 图论计数的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Codeforces Round #385 (Div. 2) C - Hongcow Builds A Nation

    题目链接:http://codeforces.com/contest/745/problem/C 题意:给出n个点m条边,还有k个不能连通的点,问最多能添加几条边. 要知道如果有n个点最多的边是n*( ...

  4. Codeforces Round #385 (Div. 2) B - Hongcow Solves A Puzzle 暴力

    B - Hongcow Solves A Puzzle 题目连接: http://codeforces.com/contest/745/problem/B Description Hongcow li ...

  5. Codeforces Round #385 (Div. 2) A. Hongcow Learns the Cyclic Shift 水题

    A. Hongcow Learns the Cyclic Shift 题目连接: http://codeforces.com/contest/745/problem/A Description Hon ...

  6. Codeforces Round #385 (Div. 1) C. Hongcow Buys a Deck of Cards

    地址:http://codeforces.com/problemset/problem/744/C 题目: C. Hongcow Buys a Deck of Cards time limit per ...

  7. Codeforces Round #385(div 2)

    A =w= B QwQ C 题意:n个点m条边的无向图,其中有k个特殊点,你在这张图上尽可能多的连边,要求k个特殊点两两不连通,问最多能连多少边 分析:并查集 对原图做一次并查集,找出特殊点所在集合中 ...

  8. Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论

    D. Vitaly and Cycle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...

  9. Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)

    题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...

随机推荐

  1. chattr&chown&cat&cut&useradd&passwd&chage&usermod

    1.用chattr命令防止系统中某个关键文件被修改 chattr +i /etc/resolv.conf chattr -i /etc/resolv.conf 要想修改此文件就要把i属性去掉 lsat ...

  2. 51NOD 1833 环

    考虑一下简单环覆盖这个图的意义,其实就是找出原序列的所有排列,满足所有<i,a[i]>都是原图中的一条有向边. 因为一个置换就是由很多简单环构成的. 于是我们可以设 f[i][S] 为考虑 ...

  3. (BruteForce)暴力破解经典题目总结

    在算法竞赛中,很多问题是来不及用数学公式推导出来的.或者说根本就找不到数学规律,这时我们就需要使用枚举来暴力破解. 不过枚举也是需要脑子的,一味的暴力只能超时.因此我这里选择了几道mooc上经典的题目 ...

  4. curl如何发送json数据?如何发送form数据?python的restfull又该如何获取这些数据?

    1.python使用flask+flask_restfull框架写的api接口,做为服务 2.curl 做为客户端发送数据 from flask import request curl发送json的方 ...

  5. 使用zerorpc踩的第一个坑:

    Server端代码:注意s.run() 和 s.run的区别,一个括号搞死我了.如果不加括号,服务端服务是不会启动的,客户端就会报连接超时的错误 Server端在本机所有IP上监听4242端口的tcp ...

  6. lfu-cache(需要O(1),所以挺难的)

    https://leetcode.com/problems/lfu-cache/ 很难,看了下面的参考: https://discuss.leetcode.com/topic/69137/java-o ...

  7. 【IE】IE对line-height 失效的的解决方案

    微软的IE9 + Extjs3.1 确实头疼.在使用了line-height:20px 的Tree的样式,可是一直没有生效, 以下给出3中解决方式: 方案1.加padding-top: <div ...

  8. vue2.0 自定义 折叠列表(Accordion)组件

    1.自定义  折叠列表 Accordion.vue (1)sass  版本 <!-- 折叠列表 组件 --> <template> <nav :class="$ ...

  9. VueJS处理逻辑指令:v-if

    HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  10. Selenium系列之--05 录制脚本并导出

    一.下载Firefox Firefox官方下载地址:http://www.firefox.com.cn/download/#more,下载延长支持版. 二.下载插件 Selenium本身有录制功能组件 ...