fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. int V , E, sz[200000] , cha[1005] , tplt;
  5. vector<vector<int>> dske(1005);
  6.  
  7. int find(int a){
  8. if(a == cha[a]) return a;
  9. return cha[a] = find(cha[a]);
  10. }
  11.  
  12. void unite(int a, int b){
  13. a = find(a);
  14. b = find(b);
  15. if(a == b) return;
  16. if(sz[a] < sz[b]) swap(a, b);
  17. cha[b] = a;
  18. sz[a] += sz[b];
  19. tplt--;
  20. return;
  21. }
  22. int main()
  23. {
  24. cin >> V >> E;
  25. for(int i = 1 ; i <= V;i++){
  26. dske[i].clear();
  27. sz[i] = 1;
  28. cha[i] = i;
  29. }
  30.  
  31. for(int i = 1 ; i <= E;i++){
  32. int a , b;cin >> a >> b;
  33. dske[a].push_back(b);
  34. dske[b].push_back(a);
  35. }
  36. int xoa[V + 1] , ans[V + 1] = {0}, act[V + 1];
  37. memset(act, 0, sizeof(act));
  38. for(int i = 1 ; i <= V;i++) cin >> xoa[i];
  39. tplt = 0;
  40. for(int i = V ; i > 0;i--){
  41. int u = xoa[i];
  42. act[u] = 1;
  43. tplt++;
  44. for(int v : dske[u]) if(act[v]) unite(u , v);
  45. ans[i] = tplt;
  46. }
  47.  
  48. for(int i = 1 ; i <= V;i++)
  49. if(ans[i] == 1) cout << "YES\n";
  50. else cout << "NO\n";
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0s 5324KB
stdin
4 4
1 2
2 3
3 4
4 1
1 2 3 4
stdout
YES
YES
YES
YES