Submission #985327

# Submission time Handle Problem Language Result Execution time Memory
985327 2024-05-17T15:32:59 Z tvladm2009 Closing Time (IOI23_closing) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 7;

vector<pair<int, int>> g[N];
int ans = 0;

vector<ll> find_dist(int n, int x) {
    vector<ll> dist(n, -1);
    queue<int> q;
    q.push(x);
    dist[x] = 0;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (auto v : g[u]) {
            if (dist[v.first] == -1) {
                dist[v.first] = dist[u] + v.second;
                q.push(v.first);
            }
        }
    }
    return dist;
}

int max_score(int n, int X, int Y, ll K, vector<int> U, vector<int> V, vector<int> W) {
    for (int i = 0; i < n; ++i) {
        g[i].clear();
    }
    ans = 0;
    for (int i = 0; i < n - 1; ++i) {
        g[U[i]].push_back(make_pair(V[i], W[i]));
        g[V[i]].push_back(make_pair(U[i], W[i]));
    }
    vector<ll> distX = find_dist(n, X);
    vector<ll> distY = find_dist(n, Y);
    vector<ll> one(n, 0), two(n, 0);
    for (int i = 0; i < n; ++i) {
        one[i] = min(distX[i], distY[i]);
        two[i] = max(distX[i], distY[i]);
    }
    one[X] = two[X];
    one[Y] = two[Y];
    two[X] = two[Y] = K + 1;
    {
        vector<ll> aux = one;
        sort(aux.begin(), aux.end());
        ll sum = 0;
        for (int i = 0; i < n; ++i) {
            if (sum + aux[i] <= K) {
                ans++;
                sum += aux[i];
            }
        }
        if (distX[Y] + 1 > 2 * K) {
            return ans;
        }
    }
    vector<bool> counted(n, 0);
    ll d = distX[Y], between = 0;
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
        if (distX[i] + distY[i] == d) {
            if (i != X && i != Y) {
                between += one[i];
            }
            cnt++;
            counted[i] = 1;
        }
    }
    ll sum_other_two = 0;
    vector<int> ord2;
    for (int i = 0; i < n; ++i) {
        if (i != X && i != Y) {
            ord2.push_back(i);
        }
    }
    sort(ord2.begin(), ord2.end(), [&](int x, int y) {
        return two[x] < two[y];
    });
    int ptr = 0;

    vector<int> ord1(n);
    iota(ord1.begin(), ord1.end(), 0);
    sort(ord1.begin(), ord1.end(), [&](int x, int y) {
        return one[x] < one[y];
    });
    auto get_sum_other_one = [&](int twos, int ones) {
        if (ones == 0) {
            return 0LL;
        }
        ll sum = 0;
        for (int i = 0; i < n; ++i) {
            if (counted[ord1[i]] == 0) {
                sum += one[ord1[i]];
                ones--;
                if (ones == 0) {
                    break;
                }
            }
        }
        if (ones > 0) {
            return K + 1;
        }
        return sum;
    };

    auto is_under = [&](int u, int v) {
        if (v == X) {
            return (distX[u] == distY[u] - distX[Y]);
        }
        if (v == Y) {
            return (distY[u] == distX[u] - distY[X]);
        }
    };

    int extra = 0;
    for (int twos = 0; twos <= n - 2; ++twos) {
        if (twos != 0) {
            sum_other_two += two[ord2[ptr]];
            if (is_under(ord2[ptr], X)) {
                if (counted[X] == 0) {
                    sum_other_two += one[X];
                    counted[X] = 1;
                    cnt--;
                    extra++;
                }
            }
            if (is_under(ord2[ptr], Y)) {
                if (counted[Y] == 0) {
                    sum_other_two += one[Y];
                    counted[Y] = 1;
                    cnt--;
                    extra++;
                }
            }
            counted[ord2[ptr]] = 1;
            if (distX[ord2[ptr]] + distY[ord2[ptr]] == d) {
                between -= one[ord2[ptr]];
                cnt--;
            }
            ptr++;
        }

        for (int ones = 0; ones + twos + cnt <= n; ++ones) {
            if (sum_other_two + between + get_sum_other_one(twos, ones) <= K) {
                // if (2 * twos + cnt + ones + 2 * extra == 32) {
                //     cout << twos << " " << cnt << " " << ones << " " << extra << "\n";
                // }
                ans = max(ans, 2 * twos + cnt + ones + 2 * extra);
            }
        }
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int N, X, Y, K;
        cin >> N >> X >> Y >> K;
        vector<int> U(N - 1, 0), V(N - 1, 0), W(N - 1, 0);
        for (int i = 0; i < N - 1; ++i) {
            cin >> U[i] >> V[i] >> W[i];
        }
        cout << max_score(N, X, Y, K, U, V, W) << "\n";
    }
    return 0;
}
/**
1
4 0 1 9
0 2 2
1 2 3
2 3 3

**/

Compilation message

closing.cpp: In lambda function:
closing.cpp:119:5: warning: control reaches end of non-void function [-Wreturn-type]
  119 |     };
      |     ^
/usr/bin/ld: /tmp/cchJBDy1.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccE7dRF5.o:closing.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status