fork(1) download
  1. #include <stdio.h>
  2.  
  3. #define N 200005
  4.  
  5. int c[N], size[N], vis[N], vid = 0;
  6.  
  7. int find(int x) {
  8. if (x != c[x])
  9. c[x] = find(c[x]);
  10. return c[x];
  11. }
  12.  
  13. int unite(int x, int y) {
  14. x = find(x);
  15. y = find(y);
  16. if (x == y) return 0;
  17. if (size[x] < size[y]) {
  18. int temp = x;
  19. x = y;
  20. y = temp;
  21. }
  22. c[y] = x;
  23. size[x] += size[y];
  24. return 1;
  25. }
  26.  
  27. void solve() {
  28. ++vid;
  29. int n;
  30. scanf("%d", &n);
  31. int s_indices[N], s_count = 0;
  32.  
  33. for (int i = 0; i < n; ++i) {
  34. int u, v;
  35. scanf("%d %d", &u, &v);
  36.  
  37. if (vis[u] != vid) {
  38. c[u] = u;
  39. size[u] = 1;
  40. vis[u] = vid;
  41. }
  42.  
  43. if (vis[v] != vid) {
  44. c[v] = v;
  45. size[v] = 1;
  46. vis[v] = vid;
  47. }
  48.  
  49. if (unite(u, v)) {
  50. s_indices[s_count++] = i + 1;
  51. }
  52. }
  53.  
  54. printf("%d\n", s_count);
  55. for (int i = 0; i < s_count; ++i)
  56. printf("%d ", s_indices[i]);
  57. printf("\n");
  58. }
  59.  
  60. int main() {
  61. int t;
  62. scanf("%d", &t);
  63. while (t--) solve();
  64. return 0;
  65. }
  66.  
Success #stdin #stdout 0s 5284KB
stdin
2
1
1 2
4
1 2
2 3
1 3
3 5
stdout
1
1 
3
1 2 4