QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#821786#8941. Even or Odd Spanning TreeSuzuranovo#WA 105ms3584kbC++233.2kb2024-12-19 18:06:362024-12-19 18:06:37

Judging History

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

  • [2024-12-19 18:06:37]
  • 评测
  • 测评结果:WA
  • 用时:105ms
  • 内存:3584kb
  • [2024-12-19 18:06:36]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 1e18;
struct node { int v, w; };
struct DSU {
    vector<int> fa;
    DSU(int n) { init(n); } void init(int n) {
        fa.resize(n); iota(fa.begin(), fa.end(), 0);
    }
    int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
    void merge(int u, int v) { fa[find(u)] = find(v); }
    bool same(int u, int v) { return find(u) == find(v); }
};
struct st {
    array<vector<node>, 20> s;
    vector<vector<node>> mp;
    vector<int> dep;
    int n;
    void init(int _n) {
        n = _n;
        for (int i = 0; i < 20; ++i)
            s[i] = vector<node>(n + 1);
        mp = vector<vector<node>>(n + 1);
        dep = vector<int>(n + 1); dep[1] = 1;
    }
    void add(int u, int v, int w) {
        mp[u].push_back({ v,w });
        mp[v].push_back({ u,w });
    }
    void build() {
        dfs(1);
        for (int b = 1; b <= __lg(n); ++b) {
            for (int u = 1; u <= n; ++u) {
                auto [v, w] = s[b - 1][u];
                s[b][u] = s[b - 1][v];
                s[b][u].w = max(s[b][u].w, w);
            }
        }
    }
    int query(int u, int v) {
        if (dep[u] < dep[v]) swap(u, v);
        int res = 0;
        while (dep[u] > dep[v]) {
            auto [x, w] = s[__lg(dep[u] - dep[v] + 1)][u];
            u = x, res = max(res, w);
        }
        if (u == v) return res;
        for (int b = __lg(dep[u]); b >= 0; --b) {
            auto [lv, lw] = s[b][u];
            auto [rv, rw] = s[b][v];
            if (lv == rv) continue;
            res = max({ res,lw,rw });
            u = lv, v = rv;
        }
        res = max({ res,s[0][u].w,s[0][v].w });
        return res;
    }
    void dfs(int u) {
        for (auto [v, w] : mp[u]) {
            if (v == s[0][u].v) continue;
            dep[v] = dep[u] + 1;
            s[0][v] = { u,w }; dfs(v);
        }
    }
};
void solve() {
    int n, m; cin >> n >> m;
    // vector<vector<node>> mp(n + 1);
    vector<array<int, 3>> e(m);
    vector<bool> vis(m);
    for (auto& [w, u, v] : e)
        cin >> u >> v >> w;
    sort(e.begin(), e.end());
    DSU d(n + 1); ll sum = 0;
    st s; s.init(n); int q = 0;
    for (int i = 0; i < m; ++i) {
        auto [w, u, v] = e[i];
        if (d.same(u, v)) continue;
        sum += w; d.merge(u, v);
        vis[i] = true; ++q;
        s.add(u, v, w);
    }
    // cerr << "buildede" << endl;
    // 判断不连通
    if (q < n - 1) {
        cout << "-1 -1\n";
        return;
    }
    s.build();
    // cerr << "builded" << endl;
    array<ll, 2> ans{ inf,inf };
    ans[sum & 1] = sum;
    for (int i = 0; i < m; ++i) {
        if (vis[i]) continue;
        auto [w, u, v] = e[i];
        int mx = s.query(u, v);
        ll nres = sum - mx + w;
        if ((nres & 1) == (sum & 1)) continue;
        ans[nres & 1] = min(ans[nres & 1], nres);
    }
    if (ans[0] == inf) ans[0] = -1;
    if (ans[1] == inf) ans[1] = -1;
    cout << ans[0] << ' ' << ans[1] << '\n';
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(0);
    int t; cin >> t;
    while (t--)
        solve();
    return 0;
}
/*
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
*/

詳細信息

Test #1:

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

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: 105ms
memory: 3548kb

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
1123022264 1180186165
2248358640 2094849259
3776889482 3738936375
1093500444 1058677117
3284228486 3402127725
1201099898 1187873655
1395482806 1334978951
3456885934 3500724419
3943951826 3988876469
2479987500 2494911351
2909126794 279...

result:

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