fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 3010;
  6.  
  7. int n, m, sz;
  8. bool closed[N];
  9. vector <int> adj[N];
  10.  
  11. void file(string fl, string in, string out) {
  12. cin.tie(0)->sync_with_stdio(0);
  13. freopen((fl + "." + in).c_str(), "r", stdin);
  14. freopen((fl + "." + out).c_str(), "w", stdout);
  15. return;
  16. }
  17.  
  18. bool BFS() {
  19. queue <int> q;
  20. int cnt = 0;
  21. bool vst[n + 10] = {0};
  22. for (int u = 1; u <= n; u++) {
  23. if (!closed[u]) {
  24. vst[u] = 1;
  25. q.push(u);
  26. break;
  27. }
  28. }
  29. while (q.size()) {
  30. int u = q.front(); q.pop();
  31. cnt++;
  32. for (int v : adj[u]) {
  33. if (!closed[v] && !vst[v]) {
  34. vst[v] = 1;
  35. q.push(v);
  36. }
  37. }
  38. }
  39. return (cnt == sz);
  40. }
  41.  
  42. int main() {
  43. file("Closing", "in", "out");
  44. cin >> n >> m;
  45. sz = n;
  46. for (int u, v, i = 1; i <= m; i++) {
  47. cin >> u >> v;
  48. adj[u].push_back(v);
  49. adj[v].push_back(u);
  50. }
  51. for (int u, i = 1; i <= n; i++) {
  52. cin >> u;
  53. cout << (BFS() ? "YES" : "NO") << "\n";
  54. closed[u] = 1;
  55. sz--;
  56. }
  57. return 0;
  58. }
Success #stdin #stdout 0.01s 5288KB
stdin
4 3
1 2
2 3
3 4
3
4
1
2
stdout
Standard output is empty