fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. const int MAX = 105;
  7. vector<int> adj[MAX];
  8. bool visited[MAX];
  9. int n;
  10.  
  11. void readInput() {
  12. cin >> n;
  13. for (int i = 1; i <= n; i++) {
  14. int k;
  15. cin >> k;
  16. for (int j = 0; j < k; j++) {
  17. int v;
  18. cin >> v;
  19. adj[i].push_back(v); // Thêm cạnh từ i đến v
  20. adj[v].push_back(i); // Thêm cạnh ngược từ v đến i (đồ thị vô hướng)
  21. }
  22. }
  23. }
  24.  
  25. void dfs(int u) {
  26. visited[u] = true;
  27. for (int v : adj[u]) {
  28. if (!visited[v]) {
  29. dfs(v);
  30. }
  31. }
  32. }
  33.  
  34. int dem_so_tplt(int ignore = -1) {
  35. memset(visited, false, sizeof(visited));
  36. int count = 0;
  37. for (int i = 1; i <= n; i++) {
  38. if (i == ignore) continue;
  39. if (!visited[i]) {
  40. count++;
  41. dfs(i);
  42. }
  43. }
  44. return count;
  45. }
  46.  
  47. void tim_dinh_tru() {
  48. vector<int> dt;
  49. int so_tplt_ban_dau = dem_so_tplt();
  50. for (int u = 1; u <= n; u++) {
  51. int tplt_moi = dem_so_tplt(u);
  52. if (tplt_moi > so_tplt_ban_dau) {
  53. dt.push_back(u);
  54. }
  55. }
  56. cout << dt.size() << endl;
  57. for (int i : dt) cout << i << " ";
  58. cout << endl;
  59. }
  60.  
  61. int main() {
  62. readInput();
  63. tim_dinh_tru();
  64. return 0;
  65. }
Success #stdin #stdout 0s 5292KB
stdin
5
2 2 3
2 1 3
3 1 2 4
2 3 5
1 4
stdout
0