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.
InputThe 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:
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|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);
}
}
}