6435 | 2023-11-29 11:39:42 | Ghost | Legkisebb nem osztható | cpp17 | Time limit exceeded 10/100 | 3.084s | 15764 KiB |
#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <stack>
using namespace std;
int main()
{
int n, temp1, temp2, i;
cin >> n;
vector<int> value(n);
for (i = 0; i < n; i++) {
cin >> value[i];
}
vector<vector<int>> map(n);
for (i = 0; i < (n - 1); i++) {
cin >> temp1 >> temp2;
temp1--; temp2--;
map[temp1].push_back(temp2);
map[temp2].push_back(temp1);
}
int k, goal, start, j;
cin >> k;
set<int> been;
queue<int> steps;
vector<int> last(n);
for (i = 0; i < k; i++) {
cin >> start >> goal;
start--; goal--;
last[start] = -1;
been.clear();
been.insert(start);
steps.push(start);
int node;
while (steps.size() > 0) {
node = steps.front();
steps.pop();
for (j = 0; j < map[node].size(); j++) {
if (!been.count(map[node][j])) {
steps.push(map[node][j]);
last[map[node][j]] = node;
}
}
been.insert(node);
}
bool correct = false;
int out = 2;
while (!correct) {
node = goal;
correct = true;
while (node != -1) {
if (out >= value[node]) {
if (out % value[node] == 0) {
correct = false;
break;
}
node = last[node];
}
else {
node = last[node];
continue;
}
}
if (correct) {
break;
}
if (++out % 2 == 0) {
out++;
}
}
cout << out << "\n";
}
}
//9
//7 25 8 4 1000000 6 11 3 2
//5 7
//5 1
//5 6
//7 3
//1 2
//1 4
//6 8
//2 9
//3
//8 9
//3 8
//4 9
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1812 KiB | ||||
subtask2 | 5/5 | ||||||
2 | Accepted | 3ms | 2060 KiB | ||||
3 | Accepted | 3ms | 2272 KiB | ||||
4 | Accepted | 4ms | 2492 KiB | ||||
5 | Accepted | 30ms | 2676 KiB | ||||
6 | Accepted | 71ms | 2892 KiB | ||||
7 | Accepted | 208ms | 2988 KiB | ||||
8 | Accepted | 328ms | 3272 KiB | ||||
9 | Accepted | 330ms | 3228 KiB | ||||
10 | Accepted | 331ms | 3444 KiB | ||||
11 | Accepted | 333ms | 3652 KiB | ||||
subtask3 | 5/5 | ||||||
12 | Accepted | 3ms | 3660 KiB | ||||
13 | Accepted | 3ms | 3512 KiB | ||||
14 | Accepted | 4ms | 3520 KiB | ||||
15 | Accepted | 28ms | 3528 KiB | ||||
16 | Accepted | 68ms | 3552 KiB | ||||
17 | Accepted | 202ms | 3868 KiB | ||||
18 | Accepted | 326ms | 3964 KiB | ||||
19 | Accepted | 328ms | 4108 KiB | ||||
20 | Accepted | 326ms | 4308 KiB | ||||
21 | Accepted | 324ms | 4316 KiB | ||||
subtask4 | 0/10 | ||||||
22 | Time limit exceeded | 3.065s | 4400 KiB | ||||
23 | Time limit exceeded | 3.055s | 4700 KiB | ||||
24 | Time limit exceeded | 3.066s | 9696 KiB | ||||
25 | Time limit exceeded | 3.071s | 13444 KiB | ||||
26 | Time limit exceeded | 3.062s | 15124 KiB | ||||
27 | Time limit exceeded | 3.063s | 15072 KiB | ||||
subtask5 | 0/10 | ||||||
28 | Time limit exceeded | 3.065s | 5060 KiB | ||||
29 | Time limit exceeded | 3.055s | 5432 KiB | ||||
30 | Time limit exceeded | 3.071s | 10192 KiB | ||||
31 | Time limit exceeded | 3.066s | 13976 KiB | ||||
32 | Time limit exceeded | 3.059s | 15316 KiB | ||||
33 | Time limit exceeded | 3.078s | 15140 KiB | ||||
subtask6 | 0/10 | ||||||
34 | Accepted | 328ms | 5028 KiB | ||||
35 | Time limit exceeded | 3.071s | 5224 KiB | ||||
36 | Time limit exceeded | 3.049s | 6760 KiB | ||||
37 | Time limit exceeded | 3.065s | 10316 KiB | ||||
38 | Time limit exceeded | 3.051s | 13968 KiB | ||||
39 | Time limit exceeded | 3.065s | 15216 KiB | ||||
40 | Time limit exceeded | 3.073s | 15196 KiB | ||||
41 | Time limit exceeded | 3.078s | 15328 KiB | ||||
subtask7 | 0/15 | ||||||
42 | Accepted | 326ms | 5288 KiB | ||||
43 | Time limit exceeded | 3.066s | 5480 KiB | ||||
44 | Time limit exceeded | 3.069s | 7084 KiB | ||||
45 | Time limit exceeded | 3.071s | 10600 KiB | ||||
46 | Time limit exceeded | 3.066s | 14268 KiB | ||||
47 | Time limit exceeded | 3.072s | 15596 KiB | ||||
48 | Time limit exceeded | 3.051s | 15596 KiB | ||||
49 | Time limit exceeded | 3.072s | 15392 KiB | ||||
50 | Time limit exceeded | 3.084s | 15564 KiB | ||||
51 | Time limit exceeded | 3.075s | 13196 KiB | ||||
52 | Time limit exceeded | 3.046s | 13232 KiB | ||||
53 | Time limit exceeded | 3.058s | 13228 KiB | ||||
54 | Time limit exceeded | 3.071s | 13016 KiB | ||||
55 | Time limit exceeded | 3.055s | 13100 KiB | ||||
56 | Time limit exceeded | 3.065s | 13188 KiB | ||||
subtask8 | 0/20 | ||||||
57 | Time limit exceeded | 3.071s | 5528 KiB | ||||
58 | Time limit exceeded | 3.039s | 6840 KiB | ||||
59 | Time limit exceeded | 3.073s | 8292 KiB | ||||
60 | Time limit exceeded | 3.065s | 10532 KiB | ||||
61 | Time limit exceeded | 3.055s | 15208 KiB | ||||
62 | Time limit exceeded | 3.049s | 15320 KiB | ||||
63 | Time limit exceeded | 3.065s | 15280 KiB | ||||
64 | Time limit exceeded | 3.065s | 15372 KiB | ||||
65 | Time limit exceeded | 3.082s | 15348 KiB | ||||
66 | Time limit exceeded | 3.078s | 15344 KiB | ||||
subtask9 | 0/25 | ||||||
67 | Time limit exceeded | 3.071s | 5464 KiB | ||||
68 | Time limit exceeded | 3.053s | 6860 KiB | ||||
69 | Time limit exceeded | 3.075s | 8352 KiB | ||||
70 | Time limit exceeded | 3.046s | 10804 KiB | ||||
71 | Time limit exceeded | 3.079s | 15368 KiB | ||||
72 | Time limit exceeded | 3.072s | 15556 KiB | ||||
73 | Time limit exceeded | 3.059s | 15668 KiB | ||||
74 | Time limit exceeded | 3.079s | 15536 KiB | ||||
75 | Time limit exceeded | 3.026s | 15524 KiB | ||||
76 | Time limit exceeded | 3.082s | 15636 KiB | ||||
77 | Time limit exceeded | 3.059s | 15588 KiB | ||||
78 | Time limit exceeded | 3.051s | 15584 KiB | ||||
79 | Time limit exceeded | 3.059s | 15688 KiB | ||||
80 | Time limit exceeded | 3.066s | 15684 KiB | ||||
81 | Time limit exceeded | 3.062s | 15764 KiB | ||||
82 | Time limit exceeded | 3.073s | 15376 KiB | ||||
83 | Time limit exceeded | 3.062s | 15660 KiB |