codeforce 385 div1 a

A. Hongcow Builds A Nation
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

题意:
给你一个无向图,有k个重要的点,这k个点不可以有直接或者间接相连,问最多可以在原有的基础上加几条边???

思路:
首先如果有n个点,那最多有(n-1)*n/2条边,那么我们就可以通过dfs找到一个连通块里面有几个点,几条边,然后再判断这个连通块有没有重要点,有就set一个新的块,没有就把他纳入到无重要点的连通块,然后最后只要选定一个点最多的含重要点的连通块连通,那答案必然最大.

ac代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;
vector<int>MAP[1010];
int n, m, t, k,sum,flag,nod;
struct node {
    int shu;
    int dian;
}arr[1010];
bool book1[1010], book2[1010];
void dfs(int a)
{
    nod++;
    if (book1[a])
        flag = 1;
    book2[a] = 1;
    sum += MAP[a].size();
    for (int i = 0; i < MAP[a].size(); i++)
    {
        if (!book2[MAP[a][i]])
        {
            dfs(MAP[a][i]);
        }
    }
}
bool cmp(node a, node b)
{
    return a.dian > b.dian;
}
int main()
{
    cin >> n >> m >> k;
    for (int i = 1; i <= k; i++)
    {
        int temp;
        cin >> temp;
        book1[temp] = 1;
    }
    for (int i = 1; i <= m; i++)
    {
        int a, b;
        cin >> a >> b;
        MAP[a].push_back(b);
        MAP[b].push_back(a);
    }
    int mark = 0;
    for (int i = 1; i <= n; i++)
    {
        if (!book2[i])
        {
            nod = 0;
            sum = 0;
            flag = 0;
            dfs(i);
            if (flag)
            {
                arr[++mark].dian = nod;
                arr[mark].shu = sum / 2;
            }
            else
            {
                arr[n+1].dian += nod;
                arr[n + 1].shu += (sum) / 2;
            }
        }
    }
    sum = 0;
    for (int i = 1; i <= mark; i++)
    {
        sum += (arr[i].dian*(arr[i].dian - 1) / 2-arr[i].shu);
    }
    if (arr[n + 1].dian != 0)
    {
        sum += (arr[n + 1].dian*(arr[n + 1].dian - 1) / 2 - arr[n + 1].shu);
        sort(arr + 1, arr + 1 + mark, cmp);
        sum += arr[1].dian*arr[n + 1].dian;
    }
    cout << sum << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值