Codeforces Round #385 (Div. 1)A. Hongcow Builds A Nation

A. Hongcow Builds A Nation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

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.

Examples
Input
4 1 2
1 3
1 2
Output
2
Input
3 3 1
2
1 2
1 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.
题意:有n个点,已知到k个点在不同的集合,给出m条边,现在要将所有的点分配到k个集合内,并且添加边把每个集合补充为完全图,问最多能添加多少条边?
思路:先把k个集合补充为完全图,再把剩余的集合添加到k个集合里点数最多的那个集合即可。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

#define maxn 1005
struct node
{
	int to, next;
}G[maxn*200];
int head[maxn], tot, step, point;
void add(int u, int v){
	G[tot].to= v;
	G[tot].next = head[u];
	head[u] = tot++;
}
bool vis[maxn], use[maxn][maxn];
int a[maxn];
void dfs(int u){
	vis[u] = 1;
	for(int i = head[u];i != -1;i = G[i].next){
		int v = G[i].to;
		if(use[u][v]||use[v][u]) continue;
		use[u][v] = use[v][u] = 1;
		step++;
		if(!vis[v]) point++;
		dfs(v);
	}
}
int main()
{
	int n, m, k;
	scanf("%d %d %d", &n, &m, &k);
	memset(head, -1, sizeof head);
	tot = 0;
	int i, j, c, p = 0;
	for(i = 0;i < k;i++){
		scanf("%d", &c);
		a[p++]=c;
	}
	int u, v;
	for(i = 0;i < m;i++){
		scanf("%d %d", &u, &v);
		add(u, v);
		add(v, u);
	}
	int ans = 0, maxsize = 0, maxi;
	for(i = 0;i < p;i++){
		step = 0;
		point = 1;
		dfs(a[i]);
		//printf("(%d %d)\n", step, point);
		ans += (point*(point-1)/2-step);
		if(maxsize < point){
			maxsize = point;
			maxi = i;
		}
	}
	//printf("-%d %d-\n", maxi, maxsize);
	int cnt = 0;
	for(i = 1;i <= n;i++){
		if(vis[i]) continue;
		step = 0;
		point = 1;
		dfs(i);
		ans += (point*(point-1)/2-step);
		ans += (point*maxsize);
		maxsize += point;
	}
	printf("%d\n", ans);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值