-
Notifications
You must be signed in to change notification settings - Fork 0
/
SRM589-D1-500.cpp
189 lines (109 loc) · 3.77 KB
/
SRM589-D1-500.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
Mohamed Ahmed Nabil
Lets try to make every gear move in a certain direction so try all combination
(L,L,R) (L,R,L) and so on
It will never be optimal to have all 3 colors in the same direction so we will only make 2 colors move in the same direction
Now Lets assume that we made R and G move in the same direction and B in the opposite direction
For each R connected to G we cant have that... However for B it doesnt matter what is it connected to as it is surely in the opposite direction
(No two similar gears mesh)
Anyways for each R and G connected to each other we want to remove the mimumum number of Gears to make them all rotate
or in other words.. Keep the maximum number of gears so that no 2 gears of the different color share an edge..
(since no 2 gears of same color share and edge that is equivalent to saying Keep MAximum gears so that no any 2 gears share an edge)
That is we want the maximum independant set between the Graoh from R to G... Since B is not in the equation now we neglect it and model a graph
from A parition containing all the R nodes to a partition containing the G nodes.. (Basically by removing B as it doesnt matter)
We end up with a graph with 2 partitions R and G (bipartite graph)
We want to keep the maximum inpedpendant set here .. or since the indepentant set is the compliment of min vertex cover
We want to remove the min vertex cover
Try for all different combination R and G , R and B , G and B and return the minumum
*/
#include <bits/stdc++.h>
#define lp(i,n) for(int i=0; i<n; i++)
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define ff first
#define ss second
#define nl "\n"
#define EPS 1e-8
#define OO 100000000
#define on(i,n) i=i|(1<<n)
#define off(i,n) i=i& (~(1<<n))
using namespace std;
const int MAXN=500;
vector<string> graphg;
string colorg;
vector<int> path;
int adjmatrix[500][500];
int visited[500];
void constructgraph(char a, char b){
memset(adjmatrix,0,sizeof adjmatrix);
int s=colorg.size();
int t=colorg.size()+1;
for(int i=0; i<colorg.size(); i++){
if(colorg[i]==a){
adjmatrix[s][i]=1;
for(int j=0; j<colorg.size(); j++){
if(colorg[j]==b && graphg[i][j]=='Y'){
adjmatrix[i][j]=1; adjmatrix[j][t]=1;
}
}
}
}
}
int find_path(int src, int sink, int curr, int w){
path.pb(curr);
visited[curr]=1;
if (curr==sink){
return w;
}
int r=0;
for(int i=0; i<MAXN;i++){
if(adjmatrix[curr][i]==0 || visited[i]) continue;
r=find_path(src,sink,i,min(w,adjmatrix[curr][i]));
if(r) break;
}
if(r!=0){
return r;
}
path.pop_back();
return r;
}
int ford_fulkrson(int src, int sink){
int f=0;
path.clear();
memset(visited,0,sizeof visited);
int w=find_path(src,sink,src, OO);
while(!path.empty()){
f+=w;
for(int i=1; i<path.size(); i++){
int m=path[i-1]; int n=path[i];
adjmatrix[m][n]-=w;
adjmatrix[n][m]+=w;
}
path.clear();
memset(visited,0,sizeof visited);
w=find_path(src,sink,src, OO);
}
return f;
}
class GearsDiv1{
public:
int getmin(string color, vector <string> graph){
colorg=color;
graphg=graph;
int s=colorg.size();
int t= colorg.size()+1;
vector<int> ans;
constructgraph('R','G');
ans.pb(ford_fulkrson(s,t));
constructgraph('R','B');
ans.pb(ford_fulkrson(s,t));
constructgraph('B','G');
ans.pb(ford_fulkrson(s,t));
sort(ans.begin(), ans.end());
return ans[0];
}
};
int main(){
}