-
Notifications
You must be signed in to change notification settings - Fork 1
/
A.cpp
100 lines (78 loc) · 1.89 KB
/
A.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <bits/stdc++.h>
using namespace std;
#define ll long long
set <ll> node, root;
ll par[1009], sz[1009], ed[1009], city[1009];
bool mark[1009];
ll findr(ll u)
{
if(par[u] == u)
return u;
return par[u] = findr(par[u]);
}
int main()
{
ll n, m, k;
cin >> n >> m >> k;
for(ll i = 1; i <= k; i++) {
ll in;
scanf("%lld", &in);
city[i] = in;
mark[in] = 1;
}
for(ll i = 1; i <= n; i++) {
par[i] = i;
sz[i] = 1;
ed[i] = 0;
}
ll ans = 0, mx = -1, mxr = 1;
for(ll i = 1; i <= m; i++) {
ll u , v;
scanf("%lld %lld", &u, &v);
ll pu = findr(u);
ll pv = findr(v);
if(pu == pv)
ed[pu]++;
else {
par[pv] = pu;
sz[pu] += sz[pv];
ed[pu] += ed[pv] + 1;
if(sz[pu] > mx) {
mx = sz[pu];
mxr = pu;
}
}
node.insert(u);
node.insert(v);
if(mark[u])
root.insert(u);
if(mark[v])
root.insert(v);
}
if(root.size() == 0 && m != 0) {
par[ city[1] ] = mxr;
sz[mxr]++;
node.insert(city[1]);
root.insert(city[1]);
}
ll unused = n - node.size() - (k - root.size());
ll tmpe = 0, tmpn = 0;
for(ll i = 1; i <= k; i++) {
ll u = findr( city[i] );
if(ed[u] == 0)
continue;
tmpe += ed[u];
tmpn += sz[u];
}
ll tot = sz[mxr] + unused + (node.size() - tmpn);
ans += (tot * (tot - 1)) / 2 - ed[mxr] - (m - tmpe);
//cout << mxr << " " << ans << " " << tot << " " << sz[mxr] << " " << tmpn << " " << unused << endl;
for(ll i = 1; i <= k; i++) {
ll u = findr(city[i]);
if(mxr == u)
continue;
ans += (sz[u] * (sz[u] - 1)) / 2 - ed[u];
}
cout << ans << endl;
return 0;
}