QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#282042 | #6421. Degree of Spanning Tree | jrjyy | RE | 0ms | 3576kb | C++20 | 3.4kb | 2023-12-11 10:54:11 | 2023-12-11 10:54:13 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n, m;
std::cin >> n >> m;
std::vector<std::vector<int>> adj(n);
for (int i = 0; i < m; ++i) {
int u, v;
std::cin >> u >> v;
--u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
std::vector<int> fa(n, -1), dep(n), deg(n), mn(n, -1);
std::vector<std::vector<int>> e(n);
auto cmp = [&](int x, int y) {
if (x == -1 || y == -1) {
return x > y;
}
return dep[x] < dep[y];
};
int cnt = 0;
auto dfs = [&](auto self, int u) -> void {
for (auto v : adj[u]) {
if (deg[v] == 0) {
fa[v] = u;
dep[v] = dep[u] + 1;
deg[u] += 1;
deg[v] += 1;
e[u].push_back(v);
++cnt;
self(self, v);
} else if (dep[v] < dep[u]) {
mn[u] = std::min(mn[u], v, cmp);
}
}
};
dfs(dfs, 0);
assert(cnt <= n - 1);
std::vector<bool> del(n);
std::vector<std::pair<int, int>> edges;
int x = std::max_element(deg.begin(), deg.end()) - deg.begin();
if (deg[x] > n / 2) {
std::vector<std::array<int, 2>> f(n, {-1, -1});
auto dfs2 = [&](auto self, int u) -> void {
if (cmp(mn[u], x)) {
if (deg[u] + (fa[u] != x) <= n / 2) {
f[u][0] = u;
}
if (deg[u] + 1 <= n / 2) {
f[u][1] = u;
}
}
for (auto v : e[u]) {
self(self, v);
for (auto x : {0, 1}) {
if (f[v][x] != -1 && (f[u][x] == -1 || cmp(mn[f[v][x]], mn[f[u][x]]))) {
f[u][x] = f[v][x];
}
}
}
};
std::pair<int, int> mx{0, -1};
for (auto u : e[x]) {
dfs2(dfs2, u);
if (f[u][0] == -1) {
continue;
}
deg[x] -= 1;
if (mn[f[u][0]] == fa[x]) {
deg[fa[x]] += 1;
}
if (f[u][1] != -1) {
mx = std::max(
mx, std::make_pair((mn[f[u][0]] == fa[x]) + (mn[f[u][1]] != fa[x]), u));
}
}
if (fa[x] != -1) {
deg[fa[x]] -= mx.first;
}
if (*std::max_element(deg.begin(), deg.end()) > n / 2) {
std::cout << "No\n";
return;
}
for (auto u : e[x]) {
if (f[u][0] == -1) {
continue;
}
if (u == mx.second) {
del[x] = true;
edges.emplace_back(mn[f[u][0]], f[u][0]);
} else {
del[f[u][0]] = true;
edges.emplace_back(mn[f[u][0]], f[u][0]);
}
}
}
std::cout << "Yes\n";
for (int u = 1; u < n; ++u) {
if (!del[u]) {
std::cout << fa[u] + 1 << " " << u + 1 << "\n";
}
}
for (auto [u, v] : edges) {
std::cout << u + 1 << " " << v + 1 << "\n";
}
}
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3576kb
input:
2 6 9 1 2 1 3 1 4 2 3 2 4 3 4 4 5 4 6 4 6 3 4 1 3 2 3 3 3 1 2
output:
Yes 1 2 2 3 3 4 4 5 4 6 No
result:
ok 2 cases
Test #2:
score: -100
Runtime Error
input:
11140 10 15 9 6 5 7 6 5 2 3 7 5 7 5 3 10 9 7 5 5 9 1 7 5 2 8 7 5 4 3 6 2 9 19 3 7 3 9 2 8 2 8 3 6 5 1 1 8 8 9 8 3 4 8 5 5 3 1 4 3 1 3 8 6 1 3 7 4 4 3 8 8 12 20 10 2 5 5 2 4 3 3 3 3 5 11 9 2 5 5 7 12 11 3 3 3 3 5 5 3 3 1 4 6 7 11 6 8 4 5 6 12 6 5 8 18 4 2 4 3 2 4 2 4 4 3 4 8 2 2 6 7 2 4 6 2 1 4 8 7 4...