QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#814350#2169. 'S No Problem口嗨战神 (Binyang Jiang, Dayu Wang, Hejun Dong)#WA 0ms3536kbC++202.0kb2024-12-14 16:52:592024-12-14 16:53:01

Judging History

This is the latest submission verdict.

  • [2024-12-14 16:53:01]
  • Judged
  • Verdict: WA
  • Time: 0ms
  • Memory: 3536kb
  • [2024-12-14 16:52:59]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;

using ll = long long;

void solve() {
    int n;
    cin >> n;
    vector G(n + 1, vector<pair<int, int>>());
    ll all = 0, ans = 0;
    for(int i = 1; i < n; i++) {
        int a, b, w;
        cin >> a >> b >> w;
        G[a].push_back({b, w});
        G[b].push_back({a, w});
        all += 2 * w;
    }
    vector<multiset<int>> f(n + 1), g(n + 1);
    vector<ll> res(n + 1);
    auto calc = [&](int x) {
        if(f[x].size() == 0) res[x] = 0;
        else if(f[x].size() == 1) res[x] = *f[x].rbegin();
        else if(f[x].size() >= 2) {
            int mx = *f[x].rbegin();
            f[x].erase(f[x].find(mx));
            res[x] = mx + *f[x].rbegin();
            f[x].insert(mx);
        }
        if(g[x].size()) res[x] = max(res[x], (ll)*g[x].rbegin());
    };
    auto dfs1 = [&](auto self, int x, int fz) -> void {
        f[x].insert(0);
        for(auto [y, w] : G[x]) if(y != fz) {
            self(self, y, x);
            f[x].insert(*f[y].rbegin() + w);
            g[x].insert(res[y]);
        }
        calc(x);
    };
    dfs1(dfs1, 1, 0);
    ans = res[1];
    auto dfs2 = [&](auto self, int x, int fz) -> void {
        for(auto [y, w] : G[x]) if(y != fz) {
            f[x].erase(f[x].find(*f[y].rbegin() + w));
            g[x].erase(g[x].find(res[y]));
            calc(x);
            ans = max(ans, res[x] + res[y]);
            f[y].insert(*f[x].rbegin() + w);
            g[y].insert(res[x]);
            calc(y);
            self(self, y, x);
            f[y].erase(*f[x].rbegin() + w);
            g[y].erase(g[y].find(res[x]));
            calc(y);
            f[x].insert(*f[y].rbegin() + w);
            g[x].insert(res[y]);
            calc(x);
        }
    };
    dfs2(dfs2, 1, 0);
    cout << all - ans << '\n';
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t = 1;
    // cin >> t;
    while(t--) solve();
    return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3536kb

input:

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

output:

13

result:

wrong answer 1st lines differ - expected: '10', found: '13'