题目链接: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. layui-时间选择器-时间范围选择

    HTML: JS: layui.use(['laydate'],function{ }); start:就为你选择的开始日期; end:就为你选择的结束日期 此方式可选择任意范围的时间,时间格式可任意 ...

  2. 转:关于使用ImageMagick和Tesseract进行简单数字图像识别

    据说Tesseract可是世界排名第三的OCR神器,2010年又更新了3.0版本.Tesseract原先是HP写的,现在Open Source了. 下面介绍怎么用Tesseract配合ImageMag ...

  3. 唤醒你的大脑 --- javascript冒泡排序

    var a; a = [1, 2, 3, 11, 55, 5, 0, 44]; (function bubbleSort() { for (var i = 0; i <= a.length - ...

  4. 100 Most Influential Books According to Stack Overflow

    Please read this blog post to see why this is here. This data was created on 02/13/2012 20:00:00 All ...

  5. Android 沉浸式全屏

    Android 4.4 带来了沉浸式全屏体验, 在沉浸式全屏模式下, 状态栏. 虚拟按键动态隐藏, 应用可 以使用完整的屏幕空间, 按照 Google 的说法, 给用户一种 “身临其境” 的体验. A ...

  6. 【bootstrap】Bootstrap Notify的使用步骤

    Bootstrap Notify说明文档:http://bootstrap-notify.remabledesigns.com/ Bootstrap Notify的GitHub地址:https://g ...

  7. 高仿QQ6.0側滑菜单之滑动优化(二)

    好了,昨天已经实现了高仿QQ6.0的側滑大致框架.如有兴趣.能够去看下仿QQ6.0側滑之ViewDragHelper的使用(一) 可是之前的实现.仅仅是简单的能够显示和隐藏左側的菜单,可是特别生硬,并 ...

  8. iOS UI08_TableView界面传值

    实现两个界面之间内容的传递 // // MainViewController.m // UI08_TableView界面传值 // // Created by dllo on 15/8/7. // C ...

  9. update tableView contenSize

    NSIndexPath *messageIndexPath = [NSIndexPath indexPathForRow:afterRowCount-1 inSection:0];    [self. ...

  10. ThinkPHP5.0中Request请求对象的常用操作

    获取当前系统参数 // 获取当前域名 echo '获取当前域名:'.$request->domain() . '<br/>'; // 获取当前入口文件 echo '获取当前入口文件: ...