Codeforces Round #385 (Div. 2) Hongcow Builds A Nation —— 图论计数
题目链接:http://codeforces.com/contest/745/problem/C
2 seconds
256 megabytes
standard input
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.
The first line of input will contain three integers n, m 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 a single integer, the maximum number of edges Hongcow can add to the graph while keeping it stable.
4 1 2
1 3
1 2
2
3 3 1
2
1 2
1 3
2 3
0
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 —— 图论计数的更多相关文章
- 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 ...
- 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) B - Hongcow Solves A Puzzle 暴力
B - Hongcow Solves A Puzzle 题目连接: http://codeforces.com/contest/745/problem/B Description Hongcow li ...
- 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 ...
- 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 ...
- Codeforces Round #385(div 2)
A =w= B QwQ C 题意:n个点m条边的无向图,其中有k个特殊点,你在这张图上尽可能多的连边,要求k个特殊点两两不连通,问最多能连多少边 分析:并查集 对原图做一次并查集,找出特殊点所在集合中 ...
- 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 ...
- Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)
题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...
随机推荐
- eclipse项目java版本更改
然后.右键点击项目->properties->Java Compiler->....如图 最后,右键点击项目->properties->Project Facets ...
- 跳转到指定页面popToViewController用法
有人问popToViewController的用法 就写了下了 希望能帮到有需要的人 [self.navigationController popToViewController:[self.navi ...
- DevExpress.XtraGrid 【转】
http://www.cnblogs.com/zeroone/p/4574539.html DevExpress.XtraGrid控件使用 该控件类是一个表格控件,但是其具有很多方便而使用的功能,例如 ...
- 怎样用命令行管理SharePoint Feature?
普通情况下对IT管理者来说.在SharePoint Farm中维护Feature,更喜欢使用命令行实现,这样能够省去登录到详细网站的操作. 比方IT接到end user的一个需求,要开启Site Co ...
- 利用 apache ab 测试服务器性能
安装步骤:https://blog.csdn.net/ahaaaaa/article/details/51514175 在Windows系统下,打开cmd命令行窗口,定位到apache安装目录的bin ...
- 基于ACCESS和ASP的SQL多个表查询与计算统计代码(一)
近期在写几个关于"Project - Subitem - Task"的管理系统,说是系统还是有点夸大了,基本就是一个多表查询调用和insert.update的数据库操作.仅仅是出现 ...
- 如何防范SQL注入式攻击
一.什么是SQL注入式攻击? 所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或者 ...
- 基于RFS(robot framework selenium)框架模拟POST/GET请求执行自动化接口测试
打开RIDE添加测试用例 如: Settings Library Collections Library RequestsLibrary Test Cases ...
- PCIE、UART、I2C、SMBUS、SPI、eSPI、USB、PS2、CAN、SDIO等数据传输协议
M.2 wife一般支持USB.SDIO.PCIE三种传输
- AngularJs + html 5 实现 裁剪上传
直接上代码 directive.js app.directive('fileUploadersm', function () { return { restrict: 'E', transclude: ...