fork download
  1. //#pragma GCC optimize("O3", "unroll-loops")
  2. //#pragma GCC target("avx2", "bmi", "bmi2", "lzcnt", "popcnt")
  3.  
  4. #include <bits/stdc++.h>
  5. #define ldb long double
  6. //#define double ldb
  7. #define db double
  8. #define unomap unordered_map
  9. #define unoset unordered_set
  10. #define endl '\n'
  11. #define str string
  12. #define strstr stringstream
  13. #define sz(a) (int)a.size()
  14. #define ll long long
  15. //#define int ll
  16. #define pii pair <int, int>
  17. #define pll pair <ll, ll>
  18. #define Unique(a) a.resize(unique(all(a)) - a.begin())
  19. #define ull unsigned ll
  20. #define fir first
  21. #define sec second
  22. #define idc cin.ignore()
  23. #define lb lower_bound
  24. #define ub upper_bound
  25. #define all(s) s.begin(), s.end()
  26. #define rev reverse
  27. #define gcd __gcd
  28. #define pushb push_back
  29. #define popb pop_back
  30. #define pushf push_front
  31. #define popf pop_front
  32. #define mul2x(a, x) a << x
  33. #define div2x(a, x) a >> x
  34. #define lcm(a, b) (a / __gcd(a, b) * b)
  35. #define log_base(x, base) log(x) / log(base)
  36. #define debug cerr << "No errors!"; exit(0);
  37. #define forw(i, a, b) for (int i = a; i <= b; ++i)
  38. #define forw2(i, a, b) for (ll i = a; i <= b; ++i)
  39. #define fors(i, a, b) for (int i = a; i >= b; --i)
  40. #define fors2(i, a, b) for (ll i = a; i >= b; --i)
  41. #define pqueue priority_queue
  42. #define sqrt sqrtl
  43. #define i128 __int128
  44. #define popcount __builtin_popcountll
  45. #define BIT(x, i) (((x) >> (i)) & 1)
  46. #define MASK(x) ((1LL) << (x))
  47. #define want_digit(x) cout << fixed << setprecision(x);
  48. #define excuting_time 1000.0 * clock() / CLOCKS_PER_SEC
  49. using namespace std;
  50. const int MOD = 1e9 + 7; // 998244353
  51. const int inf = 1e9;
  52. const ll INF = 1e18;
  53. const int N = 5e3;
  54.  
  55. mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  56. ll random(const ll &L, const ll &R) {
  57. return uniform_int_distribution<ll> (L, R) (rng);
  58. }
  59.  
  60. int n, m, u, v, par[N + 5], sz[N + 5];
  61. unomap <int, int> cnt;
  62.  
  63. struct DSU {
  64. int find(int v) {
  65. return v == par[v] ? v : par[v] = find(par[v]);
  66. }
  67.  
  68. void join(int a, int b) {
  69. a = find(a);
  70. b = find(b);
  71. if (a != b) par[b] = a;
  72. }
  73. } dsu;
  74.  
  75. void solve() {
  76. cin >> n >> m;
  77. forw (i, 1, n) par[i] = i;
  78. while (m--) {
  79. cin >> u >> v;
  80. dsu.join(u, v);
  81. }
  82.  
  83. forw (i, 1, n) ++cnt[dsu.find(i)];
  84. cout << sz(cnt) - 1 << endl;
  85. vector <int> v;
  86. for (pii tmp : cnt) v.pushb(tmp.fir);
  87. forw (i, 1, sz(v) - 1) cout << v[0] << ' ' << v[i] << endl;
  88. }
  89.  
  90. signed main() {
  91. ios::sync_with_stdio(false), cin.tie(nullptr);
  92. srand(time(NULL));
  93. #define name "COMNET"
  94. if (fopen(name".INP", "r")) {
  95. freopen(name".INP", "r", stdin);
  96. freopen(name".OUT", "w", stdout);
  97. }
  98. bool testCase = false;
  99. int numTest = 1;
  100. // cin >> numTest;
  101. forw (i, 1, numTest) {
  102. if (testCase) cout << "Case " << i << ": ";
  103. solve();
  104. }
  105. return 0;
  106. }
  107.  
  108.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
-1