Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.94 KB | None | 0 0
  1. // Copyright 2019 SD_Homework_Team
  2. #ifndef SOLVER_H_
  3. #define SOLVER_H_
  4. #include "functions.h"
  5.  
  6. class solver {
  7. public:
  8. std::string Noduri[MAX];
  9. std::vector<std::string> Strazi[MAX];
  10. std::queue<int> Coada;
  11. int dist[MAX];
  12. int viz[MAX] = {0};
  13. int viz2[MAX] = {0};
  14. int gN, gM;
  15.  
  16. /*int find(std::string nod, int N){
  17. int step,i;
  18. for (step = 1; step < N; step<<=1);
  19. for (i = 0; step; step>>=1){
  20. if (i + step < N && Noduri[i + step] <= nod){
  21. i += step;
  22. }
  23. }
  24. return i;
  25. }
  26. */
  27.  
  28. int find(std::string nod, int N) {
  29. int left = 0;
  30. int right = N - 1;
  31. int mid = 0;
  32.  
  33. while (left <= right) {
  34. mid = (left + right) / 2;
  35.  
  36. if (Noduri[mid] == nod) {
  37. return mid;
  38. }
  39.  
  40. if (Noduri[mid] < nod) {
  41. left = mid + 1;
  42. } else {
  43. right = mid - 1;
  44. }
  45. }
  46. return 0;
  47. }
  48.  
  49. void DFS(std::string current, int stop, int N) {
  50. int pozCurr = find(current, N);
  51. viz[pozCurr] = 1;
  52. if (viz[stop] == 1) return;
  53. for (unsigned int i = 0; i < Strazi[pozCurr].size(); i++) {
  54. if (!viz[find(Strazi[pozCurr][i], N)]) {
  55. DFS(Strazi[pozCurr][i], stop, N);
  56. }
  57. }
  58. }
  59.  
  60. void Dijkstra(int node, int N,int stop) {
  61. for (int i = 0; i < N; i++) {
  62. dist[i] = INF;
  63. }
  64. dist[node] = 0;
  65. viz[node] = 1;
  66. if (node == stop) return;
  67. if (viz[stop] == 1) return;
  68. Coada.push(node);
  69. while (!Coada.empty()) {
  70. int nodCur = Coada.front();
  71. Coada.pop();
  72. viz[nodCur] = 1;
  73. for (unsigned int i = 0; i < Strazi[nodCur].size(); i++) {
  74. int vecin = find(Strazi[nodCur][i], N);
  75. if (dist[nodCur] + 1< dist[vecin]) {
  76. dist[vecin] = dist[nodCur] + 1;
  77. if (!viz[vecin]) {
  78. Coada.push(vecin);
  79. viz[vecin] = 1;
  80. }
  81. }
  82. }
  83. }
  84. }
  85.  
  86. void task1_solver(std::ifstream& fin, std::ofstream& fout) {
  87. int N, M;
  88. std::string a, b;
  89. fin >> N;
  90. fin >> M;
  91. gN = N;
  92. gM = M;
  93. for (int i = 0; i < N; i++) {
  94. fin >> Noduri[i];
  95. }
  96. for (int i = 0; i < M; i++) {
  97. fin >> a >> b;
  98. int p1 = find(a, N);
  99. Strazi[p1].push_back(b);
  100. }
  101.  
  102. // citire lista queries
  103. int Q1 = 0;
  104. fin >> Q1;
  105.  
  106. for (int i = 0; i < Q1; i++) {
  107. fin >> a >> b;
  108. memset(viz, 0, sizeof(viz));
  109. DFS(a, find(b, N), N);
  110.  
  111. if (viz[find(b, N)]) {
  112. fout << 'y' << std::endl;
  113. } else {
  114. fout << 'n' << std::endl;
  115. }
  116. }
  117. }
  118.  
  119. void task2_solver(std::ifstream& fin, std::ofstream& fout) {
  120. std::string a, b;
  121. int N = gN;
  122. int Q2 = 0;
  123. fin >> Q2;
  124. for (int i = 0; i < Q2; i++) {
  125. fin >> a >> b;
  126. memset(dist, 0, sizeof(dist));
  127. memset(viz, 0, sizeof(viz));
  128. Dijkstra(find(a, N), N,find(b, N));
  129. if (!viz[find(b, N)]) {
  130. fout << "-1" << endl;
  131. } else {
  132. fout << dist[find(b, N)] << endl;
  133. }
  134. }
  135. }
  136.  
  137. void task3_solver(std::ifstream& fin, std::ofstream& fout) {
  138. char character;
  139. int decision, Q3 = 0, okAB = 0, okBA = 0, N = gN, ans1, ans2, answer;
  140. std::string a, b,c;
  141. fin >> Q3;
  142. for (int i = 0; i < Q3; i++) {
  143. fin >> character >> a >> b >> decision;
  144. if (character == 'c' && decision == 0) {
  145. // c a b 0
  146. // pushback in vecinii lui a pe b
  147. for (unsigned int i = 0; i < Strazi[find(a,N)].size(); i++) {
  148. Strazi[find(a, N)].push_back(b);
  149. }
  150. }
  151.  
  152. if (character == 'c' && decision == 1) {
  153. // c a b 1
  154. // te duci prin toti vecinii lui a si vezi daca exista b ca si vecin.
  155. // daca Strazi[find(a)][i] == find(b) atunci faci Strazi.erase(i)
  156. // te duci prin toti vecinii lui b si vezi daca exista a ca si vecin.
  157. // daca Strazi[find(b)][i] == find(a) atunci faci Strazi.erase(i)
  158. for (unsigned int i = 0; i < Strazi[find(a, N)].size();i++) {
  159. if (Strazi[find(a, N)][i] == b) {
  160. Strazi[i].erase(Strazi[i].begin() + i);
  161. }
  162. }
  163. for (unsigned int i = 0; i < Strazi[find(b, N)].size();) {
  164. if (Strazi[find(b, N)][i] == a) {
  165. Strazi[i].erase(Strazi[i].begin() + i);
  166. }
  167. }
  168. }
  169. if (character == 'c' && decision == 2) {
  170. // c a b 2
  171. // te uiti prin toti vecinii lui a, daca il are vecin pe b -> iti faci
  172. // un okAB = 1 daca se intampla asta te uiti prin toti vecinii lui b,
  173. // daca il are vecin pe a -> iti faci un okBA = 1 daca se intampla asta
  174. // daca okAB == 1 && okBA == 1 nu faci nimic
  175. // daca okAB == 1 && okBA == 0 faci muchie intre B si A adica
  176. // Strazi[find(b)].push_back(find(a))
  177. for (unsigned int i = 0; i < Strazi[find(a, N)].size(); i++) {
  178. if (Strazi[find(a, N)][i] == b) {
  179. okAB = 1;
  180. } else
  181. okAB = 0;
  182. }
  183. for (unsigned int i = 0; i < Strazi[find(b, N)].size(); i++) {
  184. if (Strazi[find(b, N)][i] == a) {
  185. okBA = 1;
  186. } else
  187. okBA = 0;
  188. }
  189. if (okAB == 1 && okBA == 0) {
  190. Strazi[find(b, N)].push_back(a);
  191. }
  192. if (okAB == 0 && okBA == 1) {
  193. Strazi[find(a, N)].push_back(b);
  194. }
  195. if (okAB == 0 && okBA == 0){
  196. Strazi[find(a,N)].push_back(b);
  197. Strazi[find(b,N)].push_back(a);
  198. }
  199. }
  200. if (character == 'c' && decision == 3) {
  201. for (unsigned int i = 0; i < Strazi[find(a, N)].size(); i++) {
  202. if (Strazi[find(a, N)][i] == b) okAB = 1;
  203. else okAB = 0;
  204. }
  205. for (unsigned int i = 0; i < Strazi[find(b, N)].size(); i++) {
  206. if (Strazi[find(b, N)][i] == a) okBA = 1;
  207. else okBA = 0;
  208. }
  209. if (okAB == 1 && okBA == 1){}
  210. if (okAB == 1 && okBA == 0){
  211. for (unsigned int i = 0; i < Strazi[find(a,N)].size();i++){
  212. Strazi[find(a,N)].erase(Strazi[find(a,N)].begin() + find(b,N));
  213. Strazi[find(b,N)].push_back(a);
  214. }
  215. }
  216. if (okBA == 1 && okAB == 0){
  217. for (unsigned int i = 0; i < Strazi[find(b,N)].size();i++){
  218. Strazi[find(b,N)].erase(Strazi[find(b,N)].begin() + find(a,N));
  219. Strazi[find(a,N)].push_back(b);
  220. }
  221. }
  222. }
  223. if (character == 'q' && decision == 0) {
  224. // q a b 0
  225. // apelezi verificarea de exista a muchiei cu DFS din task 1
  226. memset(viz, 0, sizeof(viz));
  227. DFS(a, find(b, N), N);
  228.  
  229. if (viz[find(b, N)]) {
  230. fout << 'y' << std::endl;
  231. } else {
  232. fout << 'n' << std::endl;
  233. }
  234. }
  235. if (character == 'q' && decision == 1) {
  236. memset(dist, 0, sizeof(dist));
  237. memset(viz, 0, sizeof(viz));
  238. Dijkstra(find(a, N), N,find(b, N));
  239. if (!viz[find(b, N)]) {
  240. fout << "-1" << endl;
  241. } else {
  242. fout << dist[find(b, N)] << endl;
  243. }
  244. }
  245. if (character == 'q' && decision == 2) {
  246. fin >> c;
  247. memset(viz,0,sizeof(viz));
  248. DFS(a,find(c,N),N);
  249. if (viz[find(c,N)] == 1){
  250. DFS(c,find(b,N),N);
  251. if (viz[find(b,N)] == 1){
  252. Dijkstra(find(a,N),N,find(b, N));
  253. ans1 = dist[find(c,N)];
  254. Dijkstra(find(c,N),N,find(b, N));
  255. ans2 = dist[find(b,N)];
  256. answer = ans1 + ans2;
  257. fout << answer << std::endl;
  258. }
  259. else {
  260. fout << -1 << std::endl;
  261. }
  262. }
  263. else {
  264. fout << -1 << std::endl;
  265. }
  266. }
  267. }
  268.  
  269. }
  270.  
  271. void task4_solver(std::ifstream & fin, std::ofstream & fout) {}
  272.  
  273. void task5_solver(std::ifstream & fin, std::ofstream & fout) {}
  274. };
  275.  
  276. #endif // SOLVER_H_
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement