C. Hongcow Builds A Nation
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 k countries 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.
1 #include<iostream>
2 #include<string.h>
3 #include<algorithm>
4 #include<queue>
5 #include<math.h>
6 #include<stdlib.h>
7 #include<stack>
8 #include<stdio.h>
9 #include<ctype.h>
10 #include<map>
11 #include<vector>
12 using namespace std;
13 typedef long long LL;
14 int c[100005];
15 int bin[100005];
16 int du[100005];
17 vector<int>vec[100005];
18 int fin(int x);
19 int ask[100005];
20 int id[100005];
21 map<int,int>my;
22 LL bian[100005];
23 int main(void)
24 {
25 int n,m,k;
26 int i,j;
27
28 while(scanf("%d %d %d",&n,&m,&k)!=EOF)
29 {
30 my.clear();
31 memset(bian,0,sizeof(bian));
32 for(i = 0; i <100005; i++)vec[i].clear(),bin[i] = i,du[i] = 1;
33 for(i = 1; i <=k; i++)
34 scanf("%d",&c[i]);
35 while(m--)
36 {
37 int x,y;
38 scanf("%d %d",&x,&y);
39 vec[x].push_back(y);
40 vec[y].push_back(x);
41 int xx = fin(x);
42 int yy = fin(y);
43 if(xx!=yy)
44 {
45 if(du[xx] > du[yy])
46 {
47 du[xx] += du[yy],bin[yy] = xx;
48 bian[xx]+=bian[yy];
49 bian[xx]++;
50 }
51 else
52 {
53 du[yy] += du[xx],bin[xx] = yy;
54 bian[yy]+=bian[xx];
55 bian[yy]++;
56 }
57 }
58 else bian[xx]++;
59 }
60 for(i = 1; i <= n; i++)
61 {
62 ask[i] = fin(i);
63 }
64 LL maxx = 0;
65 LL b;
66 for(i = 1; i <= k; i++)
67 {
68 my[ask[c[i]]] = 1;
69 if(du[ask[c[i]]]>maxx)
70 {
71 maxx = max((LL)du[ask[c[i]]],maxx);
72 b = bian[ask[c[i]]];
73 }
74 }
75 LL cnt = 0;
76 LL mc = 0;
77 for(i = 1; i <= n; i++)
78 {
79 if(!my.count(ask[i]))
80 {
81 cnt+=du[ask[i]];
82 my[ask[i]] = 1;
83 mc+=bian[ask[i]];
84 }
85 }//printf("%lld\n",maxx);
86 LL acc = cnt*(cnt-1)/(LL)2;
87 LL akk = acc;
88 acc+=maxx*cnt;
89 acc-=mc;
90 for(i = 1;i <= k;i++)
91 {
92 acc+=(LL)du[ask[c[i]]]*(LL)(du[ask[c[i]]]-1)/(LL)2;
93 acc-=bian[ask[c[i]]];
94 }
95 printf("%lld\n",acc);
96 }
97 return 0;
98 }
99 int fin(int x)
100 {
101 int i;
102 for(i = x; i!=bin[i];)
103 i = bin[i];
104 return i;
105 }
C. Hongcow Builds A Nation的更多相关文章
- Codeforces 744A. Hongcow Builds A Nation
A. Hongcow Builds A Nation 题意: 现在有 n 个点 ,m 条边组成了一个无向图 , 其中有 k 个特殊点, 这些特殊点之间不能连通 ,问可以再多加几条边? 因为$x^2+y ...
- 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 ...
- Codeforces 745C:Hongcow Builds A Nation(并查集)
http://codeforces.com/problemset/problem/744/A 题意:在一个图里面有n个点m条边,还有k个点是受限制的,即不能从一个受限制的点走到另外一个受限制的点(有路 ...
- C. Hongcow Builds A Nation 并查集
http://codeforces.com/contest/745/problem/C 把他们并查集后, 其他没有连去government的点,全部放去同一个并查集,然后选择一个节点数最多的gover ...
- 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) A,B,C 暴力,模拟,并查集
A. Hongcow Learns the Cyclic Shift time limit per test 2 seconds memory limit per test 256 megabytes ...
- Codeforces Round #385 //再遇状压
敲完三题挂机一小时..... 也没懂DE什么意思 rank600上了一波分... A. Hongcow Learns the Cyclic Shift 给一个字符串,每次可以把最后一个字符拿到开头 ...
- 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 ...
- cf744
Codeforces Round #385 (Div. 1) <br > A.Hongcow Builds A Nation 贪心. 显然就是凑成一个最大的块即可 那么首先并查集处理已经确 ...
随机推荐
- 48-Merge Sorted Array
$88. Merge Sorted Array My Submissions QuestionEditorial Solution Total Accepted: 98885 Total Submis ...
- linux 内存变量的分布
我们知道,linux通过虚拟内存管理进程的内存(进程的地址空间),而进程的地址空间分布如下 : 从进程的空间中可以看出,内存中的变量有的来自可执行elf文件,在elf文件中已经分配好存储空间,有的是在 ...
- 在JTable单元格上 加入组件,并赋予可编辑能力 [转]
表格(单元格放置组件) 对于JTable单元格的渲染主要是通过两个接口来实现的,一个是TableCellRenderer另一个是TableCellEditor,JTable默认是用的是DefaultC ...
- TCP中的TIME_WAIT状态
TIME_WAIT的存在有两大理由 1.可靠地实现TCP全双工连接的终止 2.允许老的可重复分节在网络中消失. 对于理由1,我们知道TCP结束需要四次挥手,若最后一次的客户端的挥手ACK丢失(假设是客 ...
- Android WifiP2p实现
Android WifiP2p实现 Wifi Direct功能早在Android 4.0就以经加入Android系统了,但是一直没有很好的被支持,主要原因是比较耗电而且连接并不是很稳定.但是也有很大的 ...
- Netty4.x 源码实战系列(一): 深入理解ServerBootstrap 与 Bootstrap (1)
从Java1.4开始, Java引入了non-blocking IO,简称NIO.NIO与传统socket最大的不同就是引入了Channel和多路复用selector的概念.传统的socket是基于s ...
- minSdkVersion、targetSdkVersion、targetApiLevel的区别
在AndroidMenifest.xml中,常常会有下面的语句: <uses-sdk android:minSdkVersion="4" android:targetSdk ...
- jmeter设置参数化
设置参数化方法有3种 第一种: 1.打开 jmeter,导入badboy录制的脚本 导入后记得选择"step"右键选择change controller ->逻辑控制器-&g ...
- NSURLSession下载文件-代理
- 3.1 涉及知识点(1)创建NSURLSession对象,设置代理(默认配置) ```objc //1.创建NSURLSession,并设置代理 /* 第一个参数:session对象的全局配置设置 ...
- 【Java】【IDE】【Jetbrain Idea】Intellij IDEA 快捷键整理
[常规] Ctrl+Shift + Enter,语句完成 "!",否定完成,输入表达式时按 "!"键 Ctrl+E,最近的文件 Ctrl+Shift+E,最近更 ...