CodeForces745C C - Hongcow Builds A Nation 图论+容斥
来源:互联网 发布:虚拟男朋友聊天软件 编辑:程序博客网 时间:2024/05/18 11:26
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 21 31 2
2
3 3 121 21 32 3
0
For the first sample test, the graph looks like this:
For the second sample test, the graph looks like this:
利用贪心,把节点加到边数最多的城市上面,然后在用容斥原理
#include <iostream> #include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <set>#include <map>#include <queue>#include <stack>#include <vector>#include <list>#define rep(i,m,n) for(i=m;i<=n;i++)#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)const int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define mod 1000000007#define vi vector<int>#define pb push_back#define mp make_pair#define fi first#define se second#define pi acos(-1.0)#define pii pair<int,int>#define Lson L, mid, rt<<1#define Rson mid+1, R, rt<<1|1const int maxn=5e2+10;using namespace std;typedef long long ll;typedef unsigned long long ull; inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}//#pragma comment(linker, "/STACK:102400000,102400000")ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}ll n,m,k,ct;ll mapp[1005][1005];ll city[1005];ll book[1005];void dfs(int beg);int main(){ios::sync_with_stdio(false); ll re = 0;cin >> n >> m >>k;for(int i=0;i<k;i++){cin >> city[i];}int u,v;for(int i=0;i<m;i++){cin >> u>>v;mapp[u][v] = 1;mapp[v][u] = 1;}ll maxx=0;for(int i=0;i<k;i++){ct = 0;book[city[i]] = 1;ct++;dfs(city[i]);re = ct*(ct-1)/2+re;/*for(int i1=1;i1<=n;i1++)cout << book[i1]<<" ";cout <<endl;*/if(maxx<ct)maxx = ct;}re -= maxx*(maxx-1)/2;ll num = 0;for(int i=1;i<=n;i++){if(book[i]==0){num++; } }maxx = num + maxx;re = maxx*(maxx-1)/2+re;cout << re-m <<endl;return 0; }void dfs(int beg){for(int i=1;i<=n;i++){if(book[i]==0&&mapp[beg][i]==1){book[i] = 1;ct ++ ;dfs(i);} }}
- CodeForces745C C - Hongcow Builds A Nation 图论+容斥
- Codeforces 745C Hongcow Builds A Nation
- CodeForces - 744A Hongcow Builds A Nation (并查集+容斥)
- Hongcow Builds A Nation CodeForces
- 【26.42%】【codeforces 745C】Hongcow Builds A Nation
- Hongcow Builds A Nation ___codeforces 744A
- Hongcow Builds A Nation ___codeforces 744A
- CodeForces - 744A Hongcow Builds A Nation
- Codeforces Round #385 (Div. 2) C. Hongcow Builds A Nation 并查集+贪心+组合学、图论、dfs
- Codeforces Round #385 (Div. 2) -- C. Hongcow Builds A Nation(并查集)
- Codeforces Round #385 (Div. 2)C. Hongcow Builds A Nation【并查集+贪心】好题~
- 745 C. Hongcow Builds A Nation codeforces (并查集)
- Codeforces 745C - Hongcow Builds A Nation 并查集乱搞,注意路径压缩
- Codeforces Round #385 (Div. 1)A. Hongcow Builds A Nation
- 并查集——Codeforces744A Hongcow Builds A Nation
- Codefocres 744A Hongcow Builds A Nation(并查集记录树上结点数)
- CodeForces 744A Hongcow Builds A Nation (并查集)
- CF745C_Hongcow Builds A Nation(并查集+贪心)
- MangoDB在java中的查询
- UMR与黑白熊
- HiveServer2 源码分析
- OkHttp的使用分析,okhttp的多种getpost方式
- gitlab 上源码Merge后出现git gc的解决方案
- CodeForces745C C - Hongcow Builds A Nation 图论+容斥
- Leetcode 133. Clone Graph (Medium) (cpp)
- 证明碰撞集问题为NP完全问题。
- 2016.12.24总结
- 直播123
- Hive CLI执行流程分析
- 约瑟夫问题 循环单链表解法
- 卷积神经网络(CNN)及其theano实现
- nginx负载均衡后tomcat获取客户端真实IP