提交时间:2025-11-19 19:47:20
运行 ID: 38933
#include <chrono> #include <iostream> #include <random> #include <unordered_set> #include <vector> using namespace std; using i64 = long long; minstd_rand0 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); inline int randint(int l, int r) { return l + rng() % (r - l + 1); } struct Solution { void main() { int n, m; cin >> n >> m; vector<vector<int>> graph(n + 1); for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; graph[u].push_back(v); graph[v].push_back(u); } for (;;) { int u = randint(1, n - 1), v = randint(u + 1, n); unordered_set<int> leaf_u(graph[u].begin(), graph[u].end()), leaf_v(graph[v].begin(), graph[v].end()), leaves = leaf_u; leaves.insert(leaf_v.begin(), leaf_v.end()); if (leaves.size() == graph[u].size() + graph[v].size() || (leaves.size() + 1 == graph[u].size() + graph[v].size() && !leaf_u.contains(v) && !leaf_v.contains(u))) { cout << "1 " << u << "\n1 " << v << '\n' << n - 2 << ' '; for (int i = 1; i <= n; ++i) { if (i != u && i != v) cout << i << ' '; } cout << '\n'; break; } } } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; while (t-- > 0) Solution().main(); return 0; }