QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#186373#2169. 'S No ProblemSo_Stuffy#WA 2ms7328kbC++201.6kb2023-09-23 18:44:442023-09-23 18:44:45

Judging History

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

  • [2023-09-23 18:44:45]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:7328kb
  • [2023-09-23 18:44:44]
  • 提交

answer

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

const int N = 1e5 + 500;

int D[N], pr[N];
vector <pair <int, int> > g[N];
int n;
map <pair <int, int>, int> d, bad, d1;

int dfs(int v, int pred = -1) {
    if (pred == -1) {
        pr[v] = -1;
    }
    int ret = v;
    for (auto [u, w] : g[v]) {
        if (u == pred) {
            continue;
        }
        D[u] = D[v] + d1[{u, v}];
        pr[u] = v;
        int c = dfs(u, v);
        if (D[c] > D[ret]) {
            ret = c;
        }
    }
    return ret;
}

int restore(int end) {
    int sum = 0;
    while (end != -1) {
        int from = pr[end];
        bad[{from, end}] = bad[{end, from}] = 1;
        sum += d[{from, end}];
        d1[{from, end}] = -1e10;
        d1[{end, from}] = -1e10;
        end = from;
    }
    return sum;
}


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

    cin >> n;

    for (int i = 0; i < n - 1; i++) {
        int a, b, w;
        cin >> a >> b >> w;
        d[{a, b}] = w;
        d1[{a, b}] = w;
        d[{b, a}] = w;
        d1[{b, a}] = w;
        g[a].push_back({b, w});
        g[b].push_back({a, w});
    }

    D[1] = 0;
    int a = dfs(1);
    D[a] = 0;
    int b = dfs(a);
    int ans = 0;
    ans += restore(b);
    D[1] = 0;
    a = dfs(1);
    D[a] = 0;
    b = dfs(a);
    ans += restore(b);

    int sum = 0;
    for (auto [p, v] : d) {
        if (bad.count(p)) {
            continue;
        }
        sum += v;
    }
    cout << ans + sum;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 6160kb

input:

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

output:

10

result:

ok single line: '10'

Test #2:

score: 0
Accepted
time: 1ms
memory: 6548kb

input:

6
6 2 1
4 6 1
3 1 1
1 5 1
1 6 10

output:

15

result:

ok single line: '15'

Test #3:

score: -100
Wrong Answer
time: 2ms
memory: 7328kb

input:

6
1 3 1
3 2 1
3 4 10
5 4 1
4 6 1

output:

16

result:

wrong answer 1st lines differ - expected: '15', found: '16'