QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#206531#7338. Education NightmareMaGnsi0WA 3314ms39732kbC++172.5kb2023-10-07 21:01:022023-10-07 21:01:03

Judging History

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

  • [2023-10-07 21:01:03]
  • 评测
  • 测评结果:WA
  • 用时:3314ms
  • 内存:39732kb
  • [2023-10-07 21:01:02]
  • 提交

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);
        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) { on_path[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 = 0;
        set<int> picked;
        multiset<int64_t> not_picked;
        for (int i = 0; i < n; ++i) {
            if (!on_path[i]) {
                not_picked.insert(dm[i]);
                ans = max(ans, ds[m - 1] + dm[i]);
            } else {
                ans = max(ans, ds[i]);
            }
        }
        if (s == m || not_picked.empty()) {
            cout << *max_element(ds.begin(), ds.end()) << "\n";
            continue;
        }
        for (int v : I) {
            if (on_path[v]) { continue; }
            while (!picked.count(v) && !on_path[v]) {
                picked.insert(v);
                not_picked.erase(not_picked.find(dm[v]));
                v = par[v];
            }
            ans = min(ans, ds[m - 1] + (not_picked.empty() ? 0 : *not_picked.rbegin()) + 2 * (int64_t)picked.size());
        }
        ans = min(ans, min(ans, ds[m - 1] + 2 * (int64_t)picked.size()));
        cout << ans << "\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
4 2 3
1 2
1 3
1 4

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: -100
Wrong Answer
time: 3314ms
memory: 39732kb

input:

14966
15 13 8
7 14
7 10
5 3
1 3
15 3
1 13
9 5
7 2
6 13
10 11
13 8
5 10
5 12
14 4
16 14 9
16 11
14 9
8 15
14 15
13 10
6 11
3 2
9 5
6 7
10 6
6 8
1 5
15 4
10 2
11 12
100 49 58
67 43
55 34
84 42
3 74
84 54
20 6
86 83
88 51
2 99
4 78
91 64
14 59
82 38
91 44
24 12
12 2
39 19
43 46
5 80
41 35
80 97
79 8
47...

output:

9
8
63
12
3
9
118
8
107
10
13
95
7
64
121
4
38
2
104
7
121
132
78
69
7
106
0
27
25
6
75
90
7
9
4
97
70
16
13
3
3
10
10
12
8
5
74
86
63
5
46
130
3
6
1
81
11
131
69
4
1
120
99
8
1
3
3
13
5
88
104
109
82
5
9
127
49
12
86
11
4
4
8
5
9
4
9
64
8
105
66
2
124
83
1
72
2
1
9
55
6
121
9
0
9
13
87
0
4
9
5
3
0
...

result:

wrong answer 7th numbers differ - expected: '131', found: '118'