QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#206825#7338. Education NightmareMaGnsi0WA 1ms3556kbC++172.3kb2023-10-07 23:03:162023-10-07 23:03:17

Judging History

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

  • [2023-10-07 23:03:17]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3556kb
  • [2023-10-07 23:03:16]
  • 提交

answer

/**
 *    author:  MaGnsi0
 *    created: 07.10.2023 12:42:52
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int T;
    cin >> T;
    while (T--) {
        int n, s, m;
        cin >> n >> s >> m;
        vector<vector<int>> adj(n);
        for (int i = 0; i < n - 1; ++i) {
            int u, v;
            cin >> u >> v;
            adj[u - 1].push_back(v - 1);
            adj[v - 1].push_back(u - 1);
        }
        vector<bool> on_path(n, false), m_sup(n, false);
        vector<int64_t> dm(n, 0), ds(n, 0), par(n, -1);
        function<void(int, int, bool)> dfs1 = [&](int v, int p, bool x) {
            bool y = x || (v == m - 1);
            if (y) { m_sup[v] = true; }
            for (int u : adj[v]) {
                if (u == p) { continue; }
                par[u] = v;
                ds[u] = ds[v] + 1;
                dfs1(u, v, y);
            }
        };
        dfs1(s - 1, -1, false);
        function<void(int, int)> dfs2 = [&](int v, int p) {
            for (int u : adj[v]) {
                if (u == p) { continue; }
                dm[u] = dm[v] + 1;
                dfs2(u, v);
            }
        };
        dfs2(m - 1, -1);
        for (int u = m - 1; u != -1; u = par[u]) {
            on_path[u] = true;
        }
        vector<int> I(n);
        iota(I.begin(), I.end(), 0);
        sort(I.begin(), I.end(), [&](int i, int j) {
                return dm[i] > dm[j];
            });
        int64_t ans = INT_MAX, x = 0;
        set<int> picked;
        multiset<int64_t> not_picked;
        for (int i = 0; i < n; ++i) {
            if (!on_path[i] && !m_sup[i]) {
                not_picked.insert(dm[i]);
                ans = max(ans, ds[m - 1] + dm[i]);
            } else {
                ans = max(ans, ds[i]);
            }
        }
        for (int v : I) {
            if (m_sup[v]) { break; }
            while (!picked.count(v) && !on_path[v]) {
                picked.insert(v);
                not_picked.erase(not_picked.find(dm[v]));
                v = par[v];
            }
            ans = min(ans, x + (not_picked.empty() ? 0 : *not_picked.rbegin()) + 2 * (int64_t)picked.size());
        }
        cout << ans << "\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3556kb

input:

1
4 2 3
1 2
1 3
1 4

output:

2

result:

wrong answer 1st numbers differ - expected: '4', found: '2'