1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| struct DSU { std::vector<int> f, siz; DSU() {} DSU(int n) { init(n); } void init(int n) { f.resize(n); std::iota(f.begin(), f.end(), 0); siz.assign(n, 1); } int find(int x) { while (x != f[x]) { x = f[x] = f[f[x]]; } return x; } bool same(int x, int y) { return find(x) == find(y); } bool merge(int x, int y) { x = find(x); y = find(y); if (x == y) { return false; } siz[x] += siz[y]; f[y] = x; return true; } int size(int x) { return siz[find(x)]; } };
constexpr int inf = 2E9;
void solve() { int n, m; std::cin >> n >> m;
std::vector<std::array<int, 3>> a(m); for (int i = 0; i < m; i++) { int u, v, w; std::cin >> u >> v >> w; u--, v--;
a[i] = {u, v, w}; }
std::sort(a.begin(), a.end(), [&](auto i, auto j) { return i[2] > j[2]; });
DSU f(n);
int U, V; int cnt = 1, ans = inf; std::vector<std::vector<int>> g(n); auto kruskal = [&]() { for (auto [u, v, w] : a) { if (!f.merge(u, v)) { ans = w; U = u, V = v; } else { g[u].push_back(v); g[v].push_back(u); } } };
kruskal();
std::vector<int> path; auto dfs = [&](auto self, int u, int fa) -> bool { if (u == V) { path.push_back(u); return true; } for (auto f : g[u]) { if (f == fa) continue ; if (self(self, f, u)) { path.push_back(u); return true; } } return false; };
dfs(dfs, U, -1);
std::cout << ans << " " << path.size() << "\n"; for (auto x : path) { std::cout << x + 1 << " "; } std::cout << "\n"; }
signed main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr);
int t; std::cin >> t;
while (t--) { solve(); }
return 0; }
|