64052023-11-28 09:59:05GhostLegkisebb nem oszthatócpp17Time limit exceeded 0/1003.101s15784 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;
            while (node != -1) {
                if (out <= value[node]) {
                    if (out % value[node] == 0 || (out > 2 && out % 2 == 0)) {
                        correct = false;
                        break;
                    }
                    correct = true;
                    node = last[node];
                }
                else {
                    break;
                }
            }
            if (correct) {
                break;
            }
            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
1Time limit exceeded3.069s1024 KiB
subtask20/5
2Time limit exceeded3.073s1300 KiB
3Time limit exceeded3.032s1564 KiB
4Time limit exceeded3.078s2424 KiB
5Accepted30ms2668 KiB
6Accepted71ms2888 KiB
7Accepted209ms3024 KiB
8Accepted330ms3328 KiB
9Accepted331ms3268 KiB
10Time limit exceeded3.058s3524 KiB
11Time limit exceeded3.065s3648 KiB
subtask30/5
12Time limit exceeded3.071s3488 KiB
13Time limit exceeded3.072s3624 KiB
14Time limit exceeded3.076s3764 KiB
15Accepted28ms3752 KiB
16Accepted68ms3860 KiB
17Accepted202ms3888 KiB
18Accepted326ms4140 KiB
19Accepted328ms4048 KiB
20Time limit exceeded3.073s3332 KiB
21Time limit exceeded3.046s3124 KiB
subtask40/10
22Time limit exceeded3.076s4268 KiB
23Time limit exceeded3.071s4584 KiB
24Time limit exceeded3.046s9284 KiB
25Time limit exceeded3.062s12956 KiB
26Time limit exceeded3.058s14416 KiB
27Time limit exceeded3.065s14520 KiB
subtask50/10
28Time limit exceeded3.035s4780 KiB
29Time limit exceeded3.04s5216 KiB
30Time limit exceeded3.049s9984 KiB
31Time limit exceeded3.066s13700 KiB
32Time limit exceeded3.071s15012 KiB
33Time limit exceeded3.062s14952 KiB
subtask60/10
34Accepted330ms4728 KiB
35Time limit exceeded3.046s4936 KiB
36Time limit exceeded3.062s6628 KiB
37Time limit exceeded3.073s10008 KiB
38Time limit exceeded3.075s13700 KiB
39Time limit exceeded3.051s14948 KiB
40Time limit exceeded3.053s14876 KiB
41Time limit exceeded3.073s14808 KiB
subtask70/15
42Accepted328ms4880 KiB
43Time limit exceeded3.062s4920 KiB
44Time limit exceeded3.069s6808 KiB
45Time limit exceeded3.071s10212 KiB
46Time limit exceeded3.059s14016 KiB
47Time limit exceeded3.066s15224 KiB
48Time limit exceeded3.085s15104 KiB
49Time limit exceeded3.058s15288 KiB
50Time limit exceeded3.026s15168 KiB
51Time limit exceeded3.055s12604 KiB
52Time limit exceeded3.055s12688 KiB
53Time limit exceeded3.046s12668 KiB
54Time limit exceeded3.066s12768 KiB
55Time limit exceeded3.063s12756 KiB
56Time limit exceeded3.071s12800 KiB
subtask80/20
57Time limit exceeded3.062s5356 KiB
58Time limit exceeded3.071s6792 KiB
59Time limit exceeded3.055s8144 KiB
60Time limit exceeded3.066s10324 KiB
61Time limit exceeded3.066s15000 KiB
62Time limit exceeded3.059s15096 KiB
63Time limit exceeded3.068s15140 KiB
64Time limit exceeded3.062s15324 KiB
65Time limit exceeded3.058s15412 KiB
66Time limit exceeded3.039s15604 KiB
subtask90/25
67Time limit exceeded3.071s5592 KiB
68Time limit exceeded3.069s6968 KiB
69Time limit exceeded3.062s8332 KiB
70Time limit exceeded3.069s10964 KiB
71Time limit exceeded3.101s15348 KiB
72Time limit exceeded3.075s15392 KiB
73Time limit exceeded3.101s15588 KiB
74Time limit exceeded3.078s15532 KiB
75Time limit exceeded3.082s15720 KiB
76Time limit exceeded3.075s15396 KiB
77Time limit exceeded3.055s15584 KiB
78Time limit exceeded3.048s15456 KiB
79Time limit exceeded3.042s15588 KiB
80Time limit exceeded3.049s15696 KiB
81Time limit exceeded3.033s15784 KiB
82Time limit exceeded3.051s15284 KiB
83Time limit exceeded3.055s15524 KiB