QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#448758#8806. Summer DrivingQwerty12321 2098ms61804kbC++235.3kb2024-06-20 03:25:172024-06-20 03:25:18

Judging History

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

  • [2024-06-20 03:25:18]
  • 评测
  • 测评结果:1
  • 用时:2098ms
  • 内存:61804kb
  • [2024-06-20 03:25:17]
  • 提交

answer

#include <bits/stdc++.h>

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

    int n, start, a, b;
    std::cin >> n >> start >> a >> b;
    start--;
    std::vector<std::vector<int>> gr(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        std::cin >> u >> v;
        u--;
        v--;

        gr[u].push_back(v);
        gr[v].push_back(u);
    }

    if (a <= b) {
        std::cout << 1 << "\n";
        return 0;
    }

    std::vector<int> depth(n), height(n), prv(n);  //, max_sb(n);
    {
        auto dfs = [&](auto dfs, int v, int f) -> void {
            prv[v] = f;
            depth[v] = f == -1 ? 0 : depth[f] + 1;
            height[v] = 0;
            for (int& t : gr[v]) {
                if (t != f) {
                    dfs(dfs, t, v);
                    height[v] = std::max(height[v], height[t] + 1);
                } else {
                    t = -1;
                }
            }
            std::erase(gr[v], -1);
        };
        dfs(dfs, start, -1);
    }
    std::vector<int> ord(n);
    std::iota(ord.begin(), ord.end(), 0);
    std::sort(ord.begin(), ord.end(), [&](int a, int b) { return height[a] < height[b]; });

    // *   dp1 - Alice, dp2 - Bob, dp3 - Alice restricted
    std::vector<int> dp1(n, -1), dp2(n, n);
    std::vector<std::vector<int>> dp3(n);

    for (int i = 0; i < n; i++) {
        dp3[i].assign(gr[i].size(), -1);
    }

    for (int i : ord) {
        int g = gr[i].size();
        {
            auto dfs0 = [&](auto dfs0, int v, int f, int d) -> int {
                if (d < 0) {
                    return n;
                }
                int val = v;
                for (int t : gr[v]) {
                    if (t != f) {
                        val = std::min(val, dfs0(dfs0, t, v, d - 1));
                    }
                }
                return val;
            };
            auto dfs1 = [&](auto dfs1, int v, int f, int d) -> int {
                if (d == 0) {
                    return dp2[v];
                }
                int val = -1;
                for (int t : gr[v]) {
                    if (t != f) {
                        val = std::max(val, dfs1(dfs1, t, v, d - 1));
                    }
                }
                return val;
            };

            if (height[i] < a) {
                std::vector<int> vec(g);
                dp1[i] = dfs0(dfs0, i, prv[i], b);
                for (int j = 0; j < g; j++) {
                    int t = gr[i][j];
                    vec[j] = std::min(i, dfs0(dfs0, t, i, b - 1));
                }
                std::array<int, 2> min = {n, n};
                for (int j = 0; j < g; j++) {
                    min[1] = std::min(min[1], vec[j]);
                    std::sort(min.begin(), min.end());
                }
                for (int j = 0; j < g; j++) {
                    int val = vec[j] == min[0] ? min[1] : min[0];
                    dp3[i][j] = val;
                }
            } else {
                dp1[i] = dfs1(dfs1, i, -1, a);
                int h1 = -1, h2 = -1;
                for (int j = 0; j < g; j++) {
                    int t = gr[i][j];
                    if (height[t] + 1 >= a) {
                        (h1 == -1 ? h1 : h2) = j;
                    }
                }
                if (h2 == -1) {
                    assert(h1 != -1);
                    // assert(false);
                    dp3[i][h1] = dfs0(dfs0, i, gr[i][h1], b);
                    int val = dfs1(dfs1, gr[i][h1], i, a - 1);
                    for (int j = 0; j < g; j++) {
                        if (j != h1) {
                            dp3[i][j] = val;
                        }
                    }
                } else {
                    std::vector<int> vec(g);
                    for (int j = 0; j < g; j++) {
                        int t = gr[i][j];
                        vec[j] = dfs1(dfs1, t, i, a - 1);
                    }
                    std::array<int, 2> max = {-1, -1};
                    for (int j = 0; j < g; j++) {
                        max[1] = std::max(max[1], vec[j]);
                        std::sort(max.rbegin(), max.rend());
                    }
                    for (int j = 0; j < g; j++) {
                        int val = vec[j] == max[0] ? max[1] : max[0];
                        dp3[i][j] = val;
                    }
                }
            }
        }
        {
            auto dfs = [&](auto dfs, int v, int f, int d, int val) {
                if (d < 0) {
                    return;
                }
                dp2[v] = std::min(dp2[v], val);
                for (int t : gr[v]) {
                    if (t != f) {
                        dfs(dfs, t, v, d - 1, val);
                    }
                }
                if (prv[v] != -1 && prv[v] != f) {
                    dfs(dfs, prv[v], v, d - 1, val);
                }
            };
            dp2[i] = std::min(dp2[i], dp1[i]);
            if (prv[i] != -1)
                dfs(dfs, prv[i], i, b - 1, dp1[i]);
            for (int j = 0; j < g; j++) {
                int t = gr[i][j];
                dfs(dfs, t, i, b - 1, dp3[i][j]);
            }
        }
    }

    std::cout << dp1[start] + 1 << "\n";

    return 0;
}

详细

Subtask #1:

score: 1
Accepted

Test #1:

score: 1
Accepted
time: 61ms
memory: 19888kb

input:

300000 110732 1 1
54285 169439
18968 45543
130988 134682
162292 70081
212010 121474
128140 292466
209394 38279
91706 225569
67647 188578
265505 84279
161782 137098
27472 221980
284973 79104
230628 268631
69945 205947
153720 168119
230161 32244
138981 44376
165008 136947
125742 123375
209131 122038
8...

output:

1

result:

ok single line: '1'

Test #2:

score: 0
Accepted
time: 56ms
memory: 19812kb

input:

300000 123141 300000 300000
35459 173656
6934 241069
183095 87288
269195 16957
19492 242321
24470 81747
25697 172188
171424 220229
160473 69937
172168 99268
220664 39397
8212 2407
46718 94855
279515 295195
205222 167038
185958 111515
172553 45818
141322 214355
61335 64490
183502 105408
234540 245525...

output:

1

result:

ok single line: '1'

Test #3:

score: 0
Accepted
time: 59ms
memory: 19636kb

input:

298765 30225 2 3
265195 252069
113697 255482
227617 218688
279488 136408
179394 139291
86777 211320
255097 13136
68860 173342
178971 175020
278041 278319
285893 289677
194438 44163
56223 283058
110392 123602
20729 89517
152134 176747
121481 243463
297305 139297
244189 117068
181785 39468
154302 1860...

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 58ms
memory: 19644kb

input:

299987 224030 2 2
177674 20066
211112 287348
150440 136779
131528 209570
208840 36580
3395 152219
89118 44403
120439 274280
267578 80200
17796 257578
229408 211795
122773 147368
139779 842
94469 299092
211457 29057
9040 117449
216268 88141
40844 98163
183412 221031
230933 237086
147633 135982
282224...

output:

1

result:

ok single line: '1'

Test #5:

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

input:

2 1 1 2
2 1

output:

1

result:

ok single line: '1'

Subtask #2:

score: 0
Time Limit Exceeded

Test #6:

score: 4
Accepted
time: 2098ms
memory: 61804kb

input:

300000 226344 300 9
32176 183340
249597 14851
145160 92372
30564 242505
1140 169463
279867 14442
266653 32911
168819 26009
138049 133460
5327 103921
262703 112512
204338 84304
98144 9089
98632 238236
79093 101104
50327 237759
61236 275195
241153 116369
86842 272794
25675 121176
110170 225753
199931 ...

output:

1

result:

ok single line: '1'

Test #7:

score: -4
Time Limit Exceeded

input:

299999 92073 2999 11
262905 260944
140896 162257
22797 193473
248112 247445
217760 68693
156294 167586
42291 233355
280566 247233
171395 239795
126564 179464
208554 185755
201665 156263
74786 85307
116366 163760
57326 143227
243541 149484
287792 283934
293052 265098
294245 296048
14582 36967
202999 ...

output:


result:


Subtask #3:

score: 0
Wrong Answer

Test #18:

score: 5
Accepted
time: 1ms
memory: 3680kb

input:

300 42 3 2
114 268
132 105
187 17
191 127
14 62
162 126
39 143
72 159
199 184
295 138
71 277
293 103
288 54
231 196
57 220
110 117
38 136
295 258
41 76
291 8
59 131
161 278
244 233
81 76
12 236
21 240
228 262
255 159
236 60
277 33
29 123
170 290
89 154
220 139
193 81
31 53
163 77
148 274
181 76
15 2...

output:

83

result:

ok single line: '83'

Test #19:

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

input:

300 178 3 2
277 106
123 105
235 290
273 34
300 180
43 55
239 74
19 138
110 201
295 18
207 97
238 177
114 24
195 219
154 186
151 294
143 291
47 293
33 99
2 46
101 39
109 240
15 256
43 121
205 261
267 257
81 167
82 23
300 182
13 46
195 221
163 17
109 93
144 23
110 17
153 129
91 243
281 74
135 72
145 1...

output:

4

result:

ok single line: '4'

Test #20:

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

input:

300 130 4 2
74 70
137 178
142 56
106 137
154 190
162 96
218 173
261 37
206 72
136 93
293 159
145 94
221 97
76 189
149 282
79 62
258 37
35 20
215 8
143 207
216 38
262 241
37 68
221 258
156 8
213 50
180 238
132 300
256 136
79 281
179 128
177 202
20 229
43 12
290 230
26 105
135 20
146 84
10 184
254 27
...

output:

126

result:

ok single line: '126'

Test #21:

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

input:

300 49 4 2
199 123
264 232
60 265
215 2
226 189
160 11
200 217
194 175
295 203
20 165
203 63
132 127
42 259
170 293
230 269
20 166
141 96
149 210
246 122
5 185
90 156
297 21
300 78
24 94
139 154
268 258
53 15
267 67
221 248
298 154
136 254
192 234
142 170
56 217
90 25
228 216
216 186
61 90
14 221
27...

output:

26

result:

ok single line: '26'

Test #22:

score: -5
Wrong Answer
time: 0ms
memory: 3616kb

input:

300 250 3 1
91 120
269 191
244 235
202 92
3 146
190 210
175 190
243 20
280 75
81 296
179 85
278 283
123 134
98 35
29 65
219 268
242 135
143 238
274 127
99 243
141 231
285 68
269 76
15 61
179 157
165 139
52 49
247 50
151 50
262 161
132 270
224 15
228 144
56 100
293 35
80 98
188 65
263 43
267 234
243 ...

output:

230

result:

wrong answer 1st lines differ - expected: '149', found: '230'

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #49:

score: 0
Wrong Answer
time: 51ms
memory: 14956kb

input:

100000 20749 3 2
89778 51257
2293 75317
20142 42260
55350 69024
2419 90402
2248 71914
60607 94307
33933 57799
79884 93934
9788 53542
18109 28742
7700 93763
12102 78825
34580 61577
84344 12887
63610 12371
30988 75638
47533 66209
95296 22495
12638 545
36347 57495
41813 49592
60342 1881
38899 62345
524...

output:

60011

result:

wrong answer 1st lines differ - expected: '43056', found: '60011'

Subtask #6:

score: 0
Skipped

Dependency #4:

0%

Subtask #7:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%