QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#556219#8941. Even or Odd Spanning Treeucup-team3519#WA 117ms5924kbC++203.2kb2024-09-10 15:58:322024-09-10 15:58:33

Judging History

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

  • [2024-09-10 15:58:33]
  • 评测
  • 测评结果:WA
  • 用时:117ms
  • 内存:5924kb
  • [2024-09-10 15:58:32]
  • 提交

answer

#include <bits/stdc++.h>

struct DSU {
    std::vector<int> fa;
    explicit DSU(int n) : fa(n) {
        std::iota(fa.begin(), fa.end(), 0);
    }
    int find(int x) {
        return fa[x] == x ? x : fa[x] = find(fa[x]);
    }
    bool merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) {
            return false;
        }
        fa[x] = y;
        return true;
    }
};

using i64 = int64_t;
constexpr i64 INF = 1e18;
constexpr int MAXN = 2e5 + 19, MAXB = 20;
int fa[MAXN][MAXB], max[MAXN][MAXB];

void solve() {
    int n, m;
    std::cin >> n >> m;

    std::vector<std::pair<int, std::pair<int, int>>> edges;
    for (int i = 1; i <= m; ++i) {
        int u, v, w;
        std::cin >> u >> v >> w;
        edges.emplace_back(w, std::pair<int, int>{u, v});
    }
    std::sort(edges.begin(), edges.end());

    i64 W = 0;
    int edge_num = 0;
    DSU dsu(n + 1);
    std::vector<std::vector<std::pair<int, int>>> T(n + 1);
    for (auto [w, p] : edges) {
        auto [u, v] = p;
        if (dsu.merge(u, v)) {
            T[u].emplace_back(v, w);
            T[v].emplace_back(u, w);
            W += w;
            edge_num += 1;
        }
    }

    if (edge_num < n - 1) {
        std::cout << "-1 -1\n";
        return;
    }

    std::vector<int> dep(n + 1);
    auto dfs = [&](auto self, int node, int f, int w) -> void {
        dep[node] = dep[f] + 1;
        fa[node][0] = f;
        max[node][0] = w;
        for (int i = 1; i < MAXB; ++i) {
            fa[node][i] = fa[fa[node][i - 1]][i - 1];
            max[node][i] = std::max(max[node][i - 1], max[fa[node][i - 1]][i - 1]);
        }
        
        for (auto [to, w] : T[node]) {
            if (to == f) {
                continue;
            }
            self(self, to, node, w);
        }
    };
    dfs(dfs, 1, 0, 0);

    auto get_max = [&](int u, int v) -> int {
        if (dep[u] < dep[v]) {
            std::swap(u, v);
        }
        int res = 0;
        for (int i = MAXB - 1; i >= 0; --i) {
            if (dep[fa[u][i]] >= dep[v]) {
                res = std::max(res, max[u][i]);
                u = fa[u][i];
            }
        }
        if (u == v) {
            return res;
        }
        for (int i = MAXB - 1; i >= 0; --i) {
            if (fa[u][i] != fa[v][i]) {
                res = std::max(res, max[u][i]);
                u = fa[u][i];
                res = std::max(res, max[v][i]);
                v = fa[v][i];
            }
        }
        return std::max({res, max[u][0], max[v][0]});
    };

    i64 min_odd = INF, min_even = INF;
    for (auto [w, p] : edges) {
        auto [u, v] = p;

        i64 res = W + w - get_max(u, v);
        if (res & 1) {
            min_odd = std::min(min_odd, res);
        } else {
            min_even = std::min(min_even, res);
        }
    }

    if (min_odd == INF) {
        min_odd = -1;
    }
    if (min_even == INF) {
        min_even = -1;
    }

    std::cout << min_even << ' ' << min_odd << '\n';
}

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

    int t;
    std::cin >> t;

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
2 1
1 2 5
3 1
1 3 1
4 4
1 2 1
1 3 1
1 4 1
2 4 2

output:

-1 5
-1 -1
4 3

result:

ok 6 numbers

Test #2:

score: -100
Wrong Answer
time: 117ms
memory: 5924kb

input:

10000
16 50
1 2 649251632
2 3 605963017
3 4 897721528
4 5 82446151
5 6 917109168
6 7 79647183
7 8 278566178
7 9 573504723
3 10 679859251
8 11 563113083
1 12 843328546
10 13 594049160
11 14 997882839
7 15 569431750
2 16 244494799
16 7 960172373
13 4 317888322
13 3 446883598
9 3 678142319
5 8 43208692...

output:

3140067932 3191118501
1262790434 1309454727
1263932600 1307926149
1189242112 1180186165
2248358640 2261370885
3776889482 3738936375
1093500444 1058677117
3433711014 3402127725
1201099898 1187873655
1395482806 1410162043
3456885934 3486092007
3943951826 3988876469
2479987500 2535532661
2909126794 293...

result:

wrong answer 2nd numbers differ - expected: '3159441841', found: '3191118501'