64042023-11-28 09:55:36GhostLegkisebb nem oszthatócpp17Time limit exceeded 0/1003.086s15964 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];
                }
            }
            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.061s1776 KiB
subtask20/5
2Time limit exceeded3.061s1968 KiB
3Time limit exceeded3.045s2232 KiB
4Time limit exceeded3.065s2712 KiB
5Accepted32ms2728 KiB
6Accepted72ms2916 KiB
7Accepted215ms3052 KiB
8Accepted337ms3216 KiB
9Accepted340ms3392 KiB
10Time limit exceeded3.066s2556 KiB
11Time limit exceeded3.062s3304 KiB
subtask30/5
12Time limit exceeded3.046s3332 KiB
13Time limit exceeded3.046s2728 KiB
14Time limit exceeded3.053s2944 KiB
15Accepted29ms3696 KiB
16Accepted70ms3840 KiB
17Accepted209ms3972 KiB
18Accepted335ms4264 KiB
19Accepted335ms4264 KiB
20Time limit exceeded3.066s4452 KiB
21Time limit exceeded3.053s3528 KiB
subtask40/10
22Time limit exceeded3.059s4480 KiB
23Time limit exceeded3.065s5056 KiB
24Time limit exceeded3.01s9684 KiB
25Time limit exceeded3.066s13252 KiB
26Time limit exceeded3.068s14832 KiB
27Time limit exceeded3.079s15060 KiB
subtask50/10
28Time limit exceeded3.078s5464 KiB
29Time limit exceeded3.046s5580 KiB
30Time limit exceeded3.026s10360 KiB
31Time limit exceeded3.055s13884 KiB
32Time limit exceeded3.082s15348 KiB
33Time limit exceeded3.062s15204 KiB
subtask60/10
34Accepted338ms5096 KiB
35Time limit exceeded3.055s5544 KiB
36Time limit exceeded3.059s7120 KiB
37Time limit exceeded3.065s10448 KiB
38Time limit exceeded3.086s14236 KiB
39Time limit exceeded3.062s15404 KiB
40Time limit exceeded3.063s15272 KiB
41Time limit exceeded3.046s15296 KiB
subtask70/15
42Accepted335ms5292 KiB
43Time limit exceeded3.033s5420 KiB
44Time limit exceeded3.049s7160 KiB
45Time limit exceeded3.071s10560 KiB
46Time limit exceeded3.071s14232 KiB
47Time limit exceeded3.059s15576 KiB
48Time limit exceeded3.058s15640 KiB
49Time limit exceeded3.049s15568 KiB
50Time limit exceeded3.063s15672 KiB
51Time limit exceeded3.075s13008 KiB
52Time limit exceeded3.063s12948 KiB
53Time limit exceeded3.062s13012 KiB
54Time limit exceeded3.062s13016 KiB
55Time limit exceeded3.055s13040 KiB
56Time limit exceeded3.062s13076 KiB
subtask80/20
57Time limit exceeded3.038s5508 KiB
58Time limit exceeded3.071s6912 KiB
59Time limit exceeded3.051s8208 KiB
60Time limit exceeded3.039s10580 KiB
61Time limit exceeded3.071s15196 KiB
62Time limit exceeded3.069s15380 KiB
63Time limit exceeded3.062s15288 KiB
64Time limit exceeded3.059s15316 KiB
65Time limit exceeded3.043s15332 KiB
66Time limit exceeded3.082s15412 KiB
subtask90/25
67Time limit exceeded3.059s5532 KiB
68Time limit exceeded3.053s6880 KiB
69Time limit exceeded3.049s8308 KiB
70Time limit exceeded3.053s10692 KiB
71Time limit exceeded3.082s15396 KiB
72Time limit exceeded3.058s15600 KiB
73Time limit exceeded3.065s15580 KiB
74Time limit exceeded3.053s15700 KiB
75Time limit exceeded3.053s15448 KiB
76Time limit exceeded3.073s15436 KiB
77Time limit exceeded3.049s15664 KiB
78Time limit exceeded3.058s15412 KiB
79Time limit exceeded3.066s15384 KiB
80Time limit exceeded3.071s15964 KiB
81Time limit exceeded3.062s15740 KiB
82Time limit exceeded3.066s15356 KiB
83Time limit exceeded3.066s15628 KiB