QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#187851#2169. 'S No ProblemSo_StuffyWA 0ms7416kbC++201.8kb2023-09-25 02:44:112023-09-25 02:44:11

Judging History

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

  • [2023-09-25 02:44:11]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:7416kb
  • [2023-09-25 02:44:11]
  • 提交

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}] = -d[{from, end}];
        d1[{end, from}] = -d[{end, from}];
        end = from;
    }
    return sum;
}


int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    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);
    // cout << a << "\n";
    D[a] = 0;
    b = dfs(a);
    // for (int i = 1; i <= n; i++) {
        // cout << D[i] << " ";
    // }
    // cout << "\n";
    // cout << b << "\n";
    ans += restore(b);

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


Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0ms
memory: 6400kb

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: 0
Accepted
time: 0ms
memory: 6032kb

input:

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

output:

15

result:

ok single line: '15'

Test #4:

score: 0
Accepted
time: 0ms
memory: 7188kb

input:

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

output:

6

result:

ok single line: '6'

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 7144kb

input:

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

output:

20

result:

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