-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path731C. Socks.cpp
60 lines (48 loc) · 949 Bytes
/
731C. Socks.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
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int const N = 2e5 + 1;
int nodes, mx;
vector<int> a;
vector<vector<int> > g;
map<int, int> freq;
bool vis[N][2];
void DFS(int n, bool type) {
vis[n][type] = 1;
if (type) {
nodes++;
mx = max(mx, ++freq[a[n]]);
}
else
freq[a[n]] = 0;
for (int i = 0; i < g[n].size(); i++)
if (!vis[g[n][i]][type])
DFS(g[n][i], type);
}
int main() {
freopen("input.txt", "r", stdin);
int n, m, k;
cin >> n >> m >> k;
a.resize(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
g.resize(n + 1);
int a, b;
for (int i = 0; i < m; i++) {
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
int res = 0;
for (int i = 0; i < n; i++) {
if (!vis[i][0]) {
nodes = mx = 0;
DFS(i, true);
res += nodes - mx;
DFS(i, false);
}
}
cout << res << endl;
return 0;
}