64352023-11-29 11:39:42GhostLegkisebb nem oszthatócpp17Time limit exceeded 10/1003.084s15764 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
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1812 KiB
subtask25/5
2Accepted3ms2060 KiB
3Accepted3ms2272 KiB
4Accepted4ms2492 KiB
5Accepted30ms2676 KiB
6Accepted71ms2892 KiB
7Accepted208ms2988 KiB
8Accepted328ms3272 KiB
9Accepted330ms3228 KiB
10Accepted331ms3444 KiB
11Accepted333ms3652 KiB
subtask35/5
12Accepted3ms3660 KiB
13Accepted3ms3512 KiB
14Accepted4ms3520 KiB
15Accepted28ms3528 KiB
16Accepted68ms3552 KiB
17Accepted202ms3868 KiB
18Accepted326ms3964 KiB
19Accepted328ms4108 KiB
20Accepted326ms4308 KiB
21Accepted324ms4316 KiB
subtask40/10
22Time limit exceeded3.065s4400 KiB
23Time limit exceeded3.055s4700 KiB
24Time limit exceeded3.066s9696 KiB
25Time limit exceeded3.071s13444 KiB
26Time limit exceeded3.062s15124 KiB
27Time limit exceeded3.063s15072 KiB
subtask50/10
28Time limit exceeded3.065s5060 KiB
29Time limit exceeded3.055s5432 KiB
30Time limit exceeded3.071s10192 KiB
31Time limit exceeded3.066s13976 KiB
32Time limit exceeded3.059s15316 KiB
33Time limit exceeded3.078s15140 KiB
subtask60/10
34Accepted328ms5028 KiB
35Time limit exceeded3.071s5224 KiB
36Time limit exceeded3.049s6760 KiB
37Time limit exceeded3.065s10316 KiB
38Time limit exceeded3.051s13968 KiB
39Time limit exceeded3.065s15216 KiB
40Time limit exceeded3.073s15196 KiB
41Time limit exceeded3.078s15328 KiB
subtask70/15
42Accepted326ms5288 KiB
43Time limit exceeded3.066s5480 KiB
44Time limit exceeded3.069s7084 KiB
45Time limit exceeded3.071s10600 KiB
46Time limit exceeded3.066s14268 KiB
47Time limit exceeded3.072s15596 KiB
48Time limit exceeded3.051s15596 KiB
49Time limit exceeded3.072s15392 KiB
50Time limit exceeded3.084s15564 KiB
51Time limit exceeded3.075s13196 KiB
52Time limit exceeded3.046s13232 KiB
53Time limit exceeded3.058s13228 KiB
54Time limit exceeded3.071s13016 KiB
55Time limit exceeded3.055s13100 KiB
56Time limit exceeded3.065s13188 KiB
subtask80/20
57Time limit exceeded3.071s5528 KiB
58Time limit exceeded3.039s6840 KiB
59Time limit exceeded3.073s8292 KiB
60Time limit exceeded3.065s10532 KiB
61Time limit exceeded3.055s15208 KiB
62Time limit exceeded3.049s15320 KiB
63Time limit exceeded3.065s15280 KiB
64Time limit exceeded3.065s15372 KiB
65Time limit exceeded3.082s15348 KiB
66Time limit exceeded3.078s15344 KiB
subtask90/25
67Time limit exceeded3.071s5464 KiB
68Time limit exceeded3.053s6860 KiB
69Time limit exceeded3.075s8352 KiB
70Time limit exceeded3.046s10804 KiB
71Time limit exceeded3.079s15368 KiB
72Time limit exceeded3.072s15556 KiB
73Time limit exceeded3.059s15668 KiB
74Time limit exceeded3.079s15536 KiB
75Time limit exceeded3.026s15524 KiB
76Time limit exceeded3.082s15636 KiB
77Time limit exceeded3.059s15588 KiB
78Time limit exceeded3.051s15584 KiB
79Time limit exceeded3.059s15688 KiB
80Time limit exceeded3.066s15684 KiB
81Time limit exceeded3.062s15764 KiB
82Time limit exceeded3.073s15376 KiB
83Time limit exceeded3.062s15660 KiB