fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Speed
  5. #define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  6.  
  7. // Typedefs
  8. #define int long long
  9. #define pb push_back
  10. #define ff first
  11. #define ss second
  12. #define all(x) (x).begin(), (x).end()
  13. #define rall(x) (x).rbegin(), (x).rend()
  14. #define sz(x) ((int)(x).size())
  15. #define endl '\n'
  16. #define yes cout << "yes\n"
  17. #define no cout << "no\n"
  18.  
  19. // Loops
  20. #define rep(i,a,b) for(int i=a;i<b;++i)
  21. #define per(i,a,b) for(int i=b-1;i>=a;--i)
  22. #define each(x, a) for (auto& x : a)
  23.  
  24. // Consts
  25. const int INF = 1e18;
  26. const int MOD = 1e9+7;
  27. const int N = 2e5 + 5;
  28.  
  29. // Math
  30. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  31. int lcm(int a, int b) { return (a / gcd(a, b)) * b; }
  32.  
  33. int power(int a, int b, int m = MOD) {
  34. int res = 1;
  35. while (b > 0) {
  36. if (b & 1) res = res * a % m;
  37. a = a * a % m;
  38. b >>= 1;
  39. }
  40. return res;
  41. }
  42.  
  43. int modinv(int a, int m = MOD) {
  44. return power(a, m - 2, m);
  45. }
  46.  
  47. // Logic
  48. void solve() {
  49. int n;
  50. cin >> n;
  51.  
  52. vector<pair<int, int>> a(n);
  53. rep(i, 0, n) cin >> a[i].ff >> a[i].ss;
  54.  
  55. vector<int> p(N);
  56. iota(all(p), 0);
  57.  
  58. auto find = [&](int x) {
  59. while (x != p[x]) x = p[x] = p[p[x]];
  60. return x;
  61. };
  62.  
  63. auto unite = [&](int x, int y) {
  64. x = find(x);
  65. y = find(y);
  66. if (x != y) {
  67. p[y] = x;
  68. return true;
  69. }
  70. return false;
  71. };
  72.  
  73. vector<int> s_ind;
  74. vector<pair<int, int>> si;
  75.  
  76. rep(i, 0, n) {
  77. int u = a[i].ff, v = a[i].ss;
  78. if (unite(u, v)) {
  79. s_ind.pb(i + 1);
  80. si.pb({min(u, v), max(u, v)});
  81. }
  82. }
  83.  
  84. cout << sz(s_ind) << endl;
  85. each(i, s_ind) cout << i << ' ';
  86. cout << endl;
  87. }
  88.  
  89. // Main
  90. int32_t main() {
  91. fast_io;
  92.  
  93. int t;
  94. cin >> t;
  95. while (t--) {
  96. solve();
  97. }
  98.  
  99. return 0;
  100. }
Success #stdin #stdout 0.01s 5308KB
stdin
2
1
1 2
4
1 2
2 3
1 3
3 5
stdout
1
1 
3
1 2 4