QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#282050#6421. Degree of Spanning TreejrjyyWA 160ms5568kbC++203.3kb2023-12-11 11:00:392023-12-11 11:00:39

Judging History

你现在查看的是最新测评结果

  • [2023-12-11 11:00:39]
  • 评测
  • 测评结果:WA
  • 用时:160ms
  • 内存:5568kb
  • [2023-12-11 11:00:39]
  • 提交

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, -1), 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];
    };
    auto dfs = [&](auto self, int u) -> void {
        for (auto v : adj[u]) {
            if (dep[v] == -1) {
                fa[v] = u;
                dep[v] = dep[u] + 1;
                deg[u] += 1;
                deg[v] += 1;
                e[u].push_back(v);
                self(self, v);
            } else if (dep[v] < dep[u]) {
                mn[u] = std::min(mn[u], v, cmp);
            }
        }
    };
    dep[0] = 0;
    dfs(dfs, 0);

    std::vector<bool> del(n), add(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(!cmp(mn[f[u][0]], fa[x]) + cmp(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 (fa[x] != -1 && u == mx.second) {
                del[x] = true;
                add[f[u][1]] = true;
            } else {
                del[u] = true;
                add[f[u][0]] = true;
            }
        }
    }

    std::cout << "Yes\n";
    for (int u = 1; u < n; ++u) {
        if (!del[u]) {
            std::cout << fa[u] + 1 << " " << u + 1 << "\n";
        }
        if (add[u]) {
            std::cout << mn[u] + 1 << " " << u + 1 << "\n";
        }
    }
}

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    int t;
    std::cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3864kb

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
Wrong Answer
time: 160ms
memory: 5568kb

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...

output:

Yes
6 2
2 3
3 4
6 5
9 6
5 7
2 8
1 9
3 10
Yes
8 2
9 3
7 4
1 5
3 6
3 7
1 8
8 9
Yes
4 2
1 3
5 4
11 5
4 6
12 7
6 8
2 9
2 10
3 11
6 12
Yes
4 2
5 3
1 4
7 5
2 6
6 7
7 8
Yes
9 2
2 3
3 4
1 5
5 6
6 7
7 8
7 9
Yes
6 2
2 3
6 4
2 5
1 6
1 7
10 8
1 9
2 10
Yes
3 2
4 3
5 4
7 5
2 6
1 7
Yes
6 2
12 3
5 4
8 5
10 6
8 7
11...

result:

wrong answer case 146, paticipant's deg[1] = 3 is too large