题解:P17484 月兔通信网络
解体思路
情况 1
考虑到最多连
选择一个点,尝试与其他所有点建边。
情况 2
当
枚举点,判断缺失了什么边;无需担心超时,因为满足条件时
特别的,重边用哈希判断即可。
参考代码
#include <iostream>
#include <unordered_map>
using namespace std;
int t, n, m;
unordered_map<int, bool> Vis;
int main(){
ios::sync_with_stdio(false);
cin >> t;
while (t--) {
Vis.clear();
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
if (u > v) swap(u, v);
Vis[u * 10000 + v] = 1;
}
if (n * (n - 1) / 2 - Vis.size() <= n - 1) {
cout << n * (n - 1) / 2 - Vis.size() << '\n';
for (int i = 1; i < n; i++) for (int j = i + 1; j <= n; j++) if (!Vis[10000 * i + j]) cout << i << ' ' << j << '\n';
} else {
int Cnt = n - 1;
for (int i = 2; i <= n; i++) if (Vis[10000 + i]) --Cnt;
cout << Cnt << '\n';
for (int i = 2; i <= n; i++) if (!Vis[10000 + i]) cout << "1 " << i << '\n';
}
}
return 0;
}