CodeForces745C C - Hongcow Builds A Nation 图论+容斥
2016-12-24 16:22
274 查看
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.
Input
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
Output a single integer, the maximum number of edges Hongcow can add to the graph while keeping it stable.
Example
Input
Output
Input
Output
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.
利用贪心,把节点加到边数最多的城市上面,然后在用容斥原理
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.
Input
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
Output a single integer, the maximum number of edges Hongcow can add to the graph while keeping it stable.
Example
Input
4 1 2 1 3 1 2
Output
2
Input
3 3 1
21 21 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.
利用贪心,把节点加到边数最多的城市上面,然后在用容斥原理
#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|1 const 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); } } }
相关文章推荐
- CF745C_Hongcow Builds A Nation(并查集+贪心)
- Codeforces 745C - Hongcow Builds A Nation 并查集乱搞,注意路径压缩
- C. Hongcow Builds A Nation 并查集
- C. Hongcow Builds A Nation
- Codeforces Round #385 (Div. 2) -- C. Hongcow Builds A Nation(并查集)
- Hongcow Builds A Nation ___codeforces 744A
- Hongcow Builds A Nation ___codeforces 744A
- 745 C. Hongcow Builds A Nation codeforces (并查集)
- Codeforces Round #385 (Div. 2) C. Hongcow Builds A Nation 并查集+贪心+组合学、图论、dfs
- 【26.42%】【codeforces 745C】Hongcow Builds A Nation
- Codeforces Round #385 (Div. 1)A. Hongcow Builds A Nation
- Codeforces Round #385 (Div. 2)C. Hongcow Builds A Nation【并查集+贪心】好题~
- B. Hongcow Solves A Puzzle
- 【CF744D】Hongcow Draws a Circle 二分+几何
- A. Hongcow Learns the Cyclic Shift
- 每日英语:Google Scraps Plan to Build Hong Kong Data Center
- Codeforces Round #385 (Div. 2) 745B Hongcow Solves A Puzzle
- Codeforces Round #385 (Div. 1) C. Hongcow Buys a Deck of Cards(DP/模拟退火)
- A. Hongcow Learns the Cyclic Shift #385
- A. Hongcow Learns the Cyclic Shift #385