Advertisement
jibha

Untitled

Feb 4th, 2022
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. class Solution {
  2. public:
  3.  
  4.  
  5.  
  6. vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
  7.  
  8. map<string,map<string,double>> m;
  9.  
  10.  
  11.  
  12. for(int i=0;i<equations.size();i++){
  13.  
  14. m[equations[i][0]][equations[i][1]]=values[i];
  15. m[equations[i][1]][equations[i][0]]=1/values[i];
  16.  
  17. for(auto iter:m[equations[i][1]]){
  18. if(iter.first!=equations[i][0]){
  19. m[equations[i][0]][iter.first]=iter.second/values[i];
  20. m[iter.first][equations[i][0]]=values[i]/(iter.second);
  21. }
  22. }
  23. for(auto iter:m[equations[i][0]]){
  24. if(iter.first!=equations[i][1]){
  25. m[equations[i][1]][iter.first]=iter.second/values[i];
  26. m[iter.first][equations[i][1]]=values[i]/(iter.second);
  27. }
  28. }
  29.  
  30. }
  31.  
  32.  
  33. for(auto iter:m){
  34.  
  35. for(auto iter1:iter.second){
  36. cout<<iter.first<<":"<<iter1.first<<":"<<iter1.second<<" ";
  37. }
  38.  
  39. }
  40.  
  41. vector<double> ans;
  42.  
  43.  
  44. for(auto iter:queries){
  45. if(m[iter[0]][iter[1]]==0.0){
  46. if(iter[1]==iter[0]){
  47. ans.push_back(1);
  48. }else{
  49. ans.push_back(-1);
  50. }
  51. }else{
  52. ans.push_back(m[iter[0]][iter[1]]);
  53. }
  54. cout<<m[iter[0]][iter[1]]<<' ';
  55. }
  56.  
  57. return ans;
  58. }
  59. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement