This repository was archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ3-2.cpp
More file actions
148 lines (124 loc) · 3.3 KB
/
Copy pathQ3-2.cpp
File metadata and controls
148 lines (124 loc) · 3.3 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <vector>
#include <unordered_map>
#include <utility>
using namespace std;
class Gloves
{
public:
Gloves(int _n,int _m,int _k,vector<int> _colors) : n(_n),m(_m),k(_k),colors(_colors)
{
};
void addEdge(int u,int v)
{
if(u == v) return;
bool inserted = false;
for(int i = 0;i < forest_gloves.size();i++)
{
if(inserted == true) break;
bool u_exists = forest_gloves[i].find(u) != forest_gloves[i].end();
bool v_exists = forest_gloves[i].find(v) != forest_gloves[i].end();
if (!u_exists && !v_exists)
continue;
//There is no difference between (u,v) and (v,u)
// if (u_exists && v_exists)
// inserted = true;
// break;
if(u_exists)
forest_gloves[i][u] += 1;
else
forest_gloves[i][u] = 1;
if(v_exists)
forest_gloves[i][v] += 1;
else
forest_gloves[i][v] = 1;
if(max_elements[i].second < forest_gloves[i][v])
max_elements[i].first = colors[v];
max_elements[i].second = forest_gloves[i][v];
if(max_elements[i].second < forest_gloves[i][u])
max_elements[i].first = colors[u];
max_elements[i].second = forest_gloves[i][u];
inserted = true;
//Might be buggy
if (!(u_exists && v_exists))
count_elements[i] += 2;
}
if(!inserted)
{
unordered_map<int,int> hash_table;
hash_table[u] = 1;
hash_table[v] = 1;
forest_gloves.push_back(hash_table);
pair<int,int> m;
m.first = colors[u-1];
m.second = 1;
max_elements.push_back(m);
count_elements.push_back(2);
}
}
int calculateMinColoring()
{
int coloring = 0;
for(int i = 0;i < forest_gloves.size();i++)
{
int max = max_elements[i].second;
coloring += (count_elements[i]-max);
}
return coloring;
}
private:
int n;
int m;
int k;
vector<int> colors;
vector<unordered_map<int,int> > forest_gloves;
vector<vector<int> > forest;
};
void run()
{
int n,m,k;
vector<int> colors;
cin >> n >> m >> k;
for(int i = 0;i < m; i++)
{
int x;
cin >> x;
colors.push_back(x);
}
Gloves g(n,m,k,colors);
while(m > 0)
{
m--;
int u,v;
cin >> u >> v;
g.addEdge(u-1,v-1);
}
cout << g.calculateMinColoring();
}
int main()
{
run();
// int n = 3;
// int m = 2;
// int k = 3;
// vector<int> colors;
// colors.push_back(1);
// colors.push_back(2);
// colors.push_back(3);
// Gloves g(n,m,k,colors);
// g.addEdge(1,2);
// g.addEdge(2,3);
// cout << g.calculateMinColoring();
// int n = 3;
// int m = 2;
// int k = 2;
// vector<int> colors;
// colors.push_back(1);
// colors.push_back(1);
// colors.push_back(2);
// Gloves g(n,m,k,colors);
// g.addEdge(1,2);
// g.addEdge(2,1);
// cout << g.calculateMinColoring();
return 0;
}