Advertisement
AdamTheGreat

Untitled

Nov 30th, 2022
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <set>
  5. #include <map>
  6. #include <stack>
  7. #include <queue>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <climits>
  11. // #include <bits/stdc++.h>
  12. using namespace std;
  13.  
  14. struct Point {
  15. int n;
  16. vector<int> neighbors;
  17. };
  18.  
  19. int main(void) {
  20. ios_base::sync_with_stdio(false);
  21. cin.tie(NULL);
  22. int n, m;
  23. cin >> n >> m;
  24. vector<Point> points(n);
  25. int a, b;
  26. for (int i = 0; i < m; i++) {
  27. cin >> a >> b;
  28. points[a-1].neighbors.push_back(b-1);
  29. points[b-1].neighbors.push_back(a-1);
  30. }
  31. vector<int> closures;
  32. for (int i = 0; i < n; i++) {
  33. cin >> a;
  34. closures.push_back(a-1);
  35. }
  36. vector<bool> visited(n, false);
  37. queue<int> q;
  38. q.push(0);
  39. visited[0] = true;
  40. while (!q.empty()) {
  41. int curr = q.front();
  42. q.pop();
  43. for (int i = 0; i < points[curr].neighbors.size(); i++) {
  44. int neighbor = points[curr].neighbors[i];
  45. if (!visited[neighbor]) {
  46. visited[neighbor] = true;
  47. q.push(neighbor);
  48. }
  49. }
  50. }
  51. bool allconnected = true;
  52. for (int i = 0; i < n; i++) {
  53. if (!visited[i]) {
  54. allconnected = false;
  55. break;
  56. }
  57. }
  58. if (allconnected) cout << "YES" << '\n'; else cout << "NO" << '\n';
  59. for (int i = 0; i < n-1; i++) {
  60. vector<int> current = points[closures[i]].neighbors;
  61. for (int j = 0; j < current.size(); j++) {
  62. // remove closures[i] from current[j]'s neighbors
  63. vector<int>::iterator it = find(points[current[j]].neighbors.begin(), points[current[j]].neighbors.end(), closures[i]);
  64. if (it != points[current[j]].neighbors.end()) {
  65. points[current[j]].neighbors.erase(it);
  66. }
  67. }
  68. points[closures[i]].neighbors.clear();
  69. // use bfs to check if all connected
  70. vector<bool> visited(n, false);
  71. int count = 1;
  72. queue<int> q;
  73. q.push(0);
  74. visited[0] = true;
  75. while (!q.empty()) {
  76. int curr = q.front();
  77. q.pop();
  78. for (int j = 0; j < points[curr].neighbors.size(); j++) {
  79. int neighbor = points[curr].neighbors[j];
  80. if (!visited[neighbor]) {
  81. count++;
  82. visited[neighbor] = true;
  83. q.push(neighbor);
  84. }
  85. }
  86. }
  87. if (count == n-i-1) cout << "YES" << '\n'; else cout << "NO" << '\n';
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement