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点 ...
随机推荐
- jvm类加载的过程
java类加载过程:加载-->验证-->准备-->解析-->初始化,之后类就可以被使用了.绝大部分情况下是按这 样的顺序来完成类的加载全过程的.但是是有例外的地方,解析也是可以 ...
- 洛谷——P1144 最短路计数
P1144 最短路计数 题目描述 给出一个N个顶点M条边的无向无权图,顶点编号为1-N.问从顶点1开始,到其他每个点的最短路有几条. 输入输出格式 输入格式: 输入第一行包含2个正整数N,M,为图的顶 ...
- MongoDb 出现配置服务不同步的处理
主要片方法就是用正常的配置文件的数据覆盖有问题的就行. 引用: http://dba.stackexchange.com/questions/48232/mongodb-config-servers- ...
- Android重写view时onAttachedToWindow () 和 onDetachedFromWindow ()
在重写View的时候,会遇到这两个方法 protected void onAttachedToWindow() Description copied from class: View This is ...
- 为什么硬盘明明还有空间,linux却说硬盘空间不足?inode;mkdir: 无法创建目录"shen1": 设备上没有空间
现象:df -h显示硬盘还有14G空间,但是touch file/mkdir directory都失败,提示硬盘没有空间 原因:df -ia查看下inode的使用情况,发现已经爆了,(下图显示使用88 ...
- C中的继承和多态
昨天同学面试被问到这个问题,很有水平,以前都没有遇到过这个问题,一时自己也不知道怎么回答. 网上学习了一下,记录以备后用! C/C++ Internals : 里面的问题都写的不错,可以读读! Ref ...
- auth 认证
参考链接 https://blog.csdn.net/hotnet522/article/details/5824716 http://blog.sina.com.cn/s/blog_6d6fbbd5 ...
- HTML5、CSS3等新特性:
HTML5:1/绘画 canvas 元素;2/用于媒介回放的 video 和 audio 元素;3/本地离线存储 localStorage 长期存储数据,浏览器关闭后数据不丢失;4/sessionSt ...
- ARM和X86
嵌入式简介汇总 脚本语言 编程语言 Java C# C ++ 汇编 机器语言 语言 Unix Linux Android + 塞班 + Windows + + + ios系统 基于unix内核的图形化 ...
- (总结)Nginx配置文件nginx.conf中文具体解释
#定义Nginx执行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错误日志定义类型,[ debug | ...