Advertisement
AdamTheGreat

Untitled

Nov 30th, 2022
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <set>
  6. #include <map>
  7. #include <stack>
  8. #include <queue>
  9. #include <cmath>
  10. #include <climits>
  11. // #include <bits/stdc++.h>
  12. using namespace std;
  13.  
  14. struct Point {
  15. int n;
  16. vector<Point*> paths;
  17. };
  18.  
  19. int n, m;
  20. string s;
  21. vector<Point> farms;
  22. vector<pair<int, int> > friendpaths;
  23. vector<char> favoritemilktype;
  24. vector<int> result;
  25.  
  26. bool friendinpath(int friend_, int a, int b) {
  27. queue<Point*> notebook;
  28. vector<bool> visited(n, false);
  29. notebook.push(&farms[a]);
  30. visited[a] = true;
  31. vector<int> path;
  32. while (!notebook.empty()) {
  33. Point* current = notebook.front();
  34. notebook.pop();
  35. for (int i = 0; i < current->paths.size(); i++) {
  36. if (!visited[current->paths[i]->n]) {
  37. visited[current->paths[i]->n] = true;
  38. notebook.push(current->paths[i]);
  39. path.push_back(current->paths[i]->n);
  40. }
  41. }
  42. path.pop_back();
  43. }
  44. return false;
  45. }
  46.  
  47. int main(void) {
  48. cin >> n >> m;
  49. cin >> s;
  50. int a, b;
  51. farms.resize(n);
  52. for (int i = 0; i < n; i++) {
  53. farms[i].n = i;
  54. }
  55. for (int i = 0; i < n-1; i++) {
  56. cin >> a >> b;
  57. farms[a-1].paths.push_back(&farms[b-1]);
  58. farms[b-1].paths.push_back(&farms[a-1]);
  59. }
  60. char tmp;
  61. for (int i = 0; i < m; i++) {
  62. cin >> a >> b >> tmp;
  63. friendpaths.push_back(make_pair(a-1, b-1));
  64. favoritemilktype.push_back(tmp);
  65. }
  66. // cout << "n m:" << endl;
  67. // cout << n << " " << m << endl;
  68. // cout << "s:" << endl;
  69. // cout << s << endl;
  70. // cout << "farms:" << endl;
  71. // for (int i = 0; i < n; i++) {
  72. // for (int j = 0; j < farms[i].paths.size(); j++) {
  73. // cout << farms[i].paths[j]+1 << " ";
  74. // }
  75. // cout << endl;
  76. // }
  77. // cout << "friendpaths:" << endl;
  78. // for (int i = 0; i < m; i++) {
  79. // cout << friendpaths[i].first+1 << " " << friendpaths[i].second+1 << " " << favoritemilktype[i] << endl;
  80. // }
  81. // if (friendinpath(0, 0, 1)) cout << "YES" << endl; else cout << "NO" << endl;
  82. for (int i = 0; i < m; i++) {
  83. if (friendinpath(i, friendpaths[i].first, friendpaths[i].second)) {
  84. result.push_back(1);
  85. } else {
  86. result.push_back(0);
  87. }
  88. }
  89. for (int i = 0; i < result.size(); i++) {
  90. cout << result[i];
  91. }
  92. cout << '\n';
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement