-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path731C_socks.cpp
56 lines (46 loc) · 1.16 KB
/
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
#include <bits/stdc++.h>
std::vector <int> graph[1000100];
std::array <bool, 1000100> visited = {0};
void dfs(int vertex, const std::vector <int> &socks_colors, std::map <int, int> &colors)
{
visited[vertex] = 1;
colors[socks_colors[vertex - 1]]++;
for (size_t i = 0; i < graph[vertex].size(); ++i)
{
if (!visited[graph[vertex][i]])
{
dfs(graph[vertex][i], socks_colors, colors);
}
}
}
int32_t main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
int socks_number, days_number, colors_number;
std::cin >> socks_number >> days_number >> colors_number;
std::vector <int> socks_colors(socks_number);
for (auto &i : socks_colors) std::cin >> i;
for (int i = 0; i < days_number; ++i)
{
int left, right;
std::cin >> left >> right;
graph[left].push_back(right);
graph[right].push_back(left);
}
int result = 0;
for (int i = 1; i <= socks_number; ++i)
{
if (!visited[i])
{
std::map <int, int> colors;
dfs(i, socks_colors, colors);
int mode = 0;
for (auto i : colors) mode = std::max(mode, i.second), result += i.second;
result -= mode;
}
}
std::cout << result << '\n';
return 0;
}