63992023-11-27 18:29:02GhostLegkisebb nem oszthatócpp17Time limit exceeded 10/1003.086s100328 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), outV;
    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);
        }

        outV.clear();
        node = goal;
        while (node != -1) {
            outV.push_back(value[node]);
            node = last[node];
        }

        bool correct = false;
        int out = 2;
        while (!correct) {
            for (j = 0; j < outV.size(); j++) {
                if (out % outV[j] == 0) {
                    correct = false;
                    break;
                }
                correct = true;
            }
            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
1Accepted3ms1812 KiB
subtask25/5
2Accepted3ms2064 KiB
3Accepted3ms2212 KiB
4Accepted4ms2308 KiB
5Accepted30ms2396 KiB
6Accepted71ms2760 KiB
7Accepted209ms3164 KiB
8Accepted330ms3328 KiB
9Accepted331ms3440 KiB
10Accepted331ms3592 KiB
11Accepted333ms3848 KiB
subtask35/5
12Accepted3ms3728 KiB
13Accepted3ms3780 KiB
14Accepted4ms3792 KiB
15Accepted28ms3892 KiB
16Accepted68ms4172 KiB
17Accepted202ms4216 KiB
18Accepted326ms4452 KiB
19Accepted326ms4532 KiB
20Accepted324ms4520 KiB
21Accepted324ms4776 KiB
subtask40/10
22Time limit exceeded3.081s5336 KiB
23Time limit exceeded3.053s6072 KiB
24Time limit exceeded3.082s11944 KiB
25Time limit exceeded3.082s17120 KiB
26Time limit exceeded3.065s20064 KiB
27Time limit exceeded3.059s21612 KiB
subtask50/10
28Time limit exceeded3.075s11624 KiB
29Time limit exceeded3.078s12328 KiB
30Time limit exceeded3.053s18000 KiB
31Time limit exceeded3.058s23072 KiB
32Time limit exceeded3.062s25824 KiB
33Time limit exceeded3.075s27204 KiB
subtask60/10
34Accepted330ms17208 KiB
35Time limit exceeded3.058s17696 KiB
36Time limit exceeded3.072s20076 KiB
37Time limit exceeded3.038s24768 KiB
38Time limit exceeded3.069s30456 KiB
39Time limit exceeded3.065s33804 KiB
40Time limit exceeded3.059s35748 KiB
41Time limit exceeded3.078s37568 KiB
subtask70/15
42Accepted328ms27048 KiB
43Time limit exceeded3.046s27448 KiB
44Time limit exceeded3.049s29760 KiB
45Time limit exceeded3.058s34228 KiB
46Time limit exceeded3.062s39728 KiB
47Time limit exceeded3.075s42840 KiB
48Time limit exceeded3.066s45148 KiB
49Time limit exceeded3.062s47380 KiB
50Time limit exceeded3.062s49168 KiB
51Time limit exceeded3.066s48648 KiB
52Time limit exceeded3.066s50036 KiB
53Time limit exceeded3.066s51892 KiB
54Time limit exceeded3.071s53584 KiB
55Time limit exceeded3.058s54944 KiB
56Time limit exceeded3.059s56496 KiB
subtask80/20
57Time limit exceeded3.075s49252 KiB
58Time limit exceeded3.046s51412 KiB
59Time limit exceeded3.062s53548 KiB
60Time limit exceeded3.069s57032 KiB
61Time limit exceeded3.086s63924 KiB
62Time limit exceeded3.066s66164 KiB
63Time limit exceeded3.071s67764 KiB
64Time limit exceeded3.051s69524 KiB
65Time limit exceeded3.035s71620 KiB
66Time limit exceeded3.078s73704 KiB
subtask90/25
67Time limit exceeded3.045s63584 KiB
68Time limit exceeded3.056s65400 KiB
69Time limit exceeded3.072s67668 KiB
70Time limit exceeded3.061s71040 KiB
71Time limit exceeded3.042s77608 KiB
72Time limit exceeded3.059s79760 KiB
73Time limit exceeded3.075s81564 KiB
74Time limit exceeded3.059s83372 KiB
75Time limit exceeded3.066s85152 KiB
76Time limit exceeded3.075s87120 KiB
77Time limit exceeded3.046s88768 KiB
78Time limit exceeded3.071s90388 KiB
79Time limit exceeded3.059s92196 KiB
80Time limit exceeded3.059s94428 KiB
81Time limit exceeded3.042s96284 KiB
82Time limit exceeded3.062s98468 KiB
83Time limit exceeded3.082s100328 KiB