64022023-11-28 09:48:37GhostLegkisebb nem oszthatócpp17Time limit exceeded 10/1003.099s15564 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] == 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
1Accepted3ms1812 KiB
subtask25/5
2Accepted3ms2004 KiB
3Accepted3ms2252 KiB
4Accepted4ms2436 KiB
5Accepted30ms2792 KiB
6Accepted70ms3024 KiB
7Accepted208ms3420 KiB
8Accepted328ms3496 KiB
9Accepted328ms3708 KiB
10Accepted330ms3748 KiB
11Accepted331ms4024 KiB
subtask35/5
12Accepted3ms3884 KiB
13Accepted3ms3996 KiB
14Accepted4ms4116 KiB
15Accepted28ms4036 KiB
16Accepted67ms4056 KiB
17Accepted202ms4100 KiB
18Accepted326ms4136 KiB
19Accepted326ms4148 KiB
20Accepted326ms4148 KiB
21Accepted324ms4300 KiB
subtask40/10
22Time limit exceeded3.062s4676 KiB
23Time limit exceeded3.059s4888 KiB
24Time limit exceeded3.082s9680 KiB
25Time limit exceeded3.073s13152 KiB
26Time limit exceeded3.065s14500 KiB
27Time limit exceeded3.046s14500 KiB
subtask50/10
28Time limit exceeded3.073s4612 KiB
29Time limit exceeded3.065s4856 KiB
30Time limit exceeded3.059s9696 KiB
31Time limit exceeded3.082s13376 KiB
32Time limit exceeded3.055s14892 KiB
33Time limit exceeded3.043s14788 KiB
subtask60/10
34Accepted328ms4536 KiB
35Time limit exceeded3.049s4852 KiB
36Time limit exceeded3.073s6460 KiB
37Time limit exceeded3.062s9716 KiB
38Time limit exceeded3.062s13504 KiB
39Time limit exceeded3.062s14672 KiB
40Time limit exceeded3.062s14568 KiB
41Time limit exceeded3.085s14860 KiB
subtask70/15
42Accepted324ms4996 KiB
43Time limit exceeded3.065s5128 KiB
44Time limit exceeded3.056s6948 KiB
45Time limit exceeded3.049s10336 KiB
46Time limit exceeded3.053s14044 KiB
47Time limit exceeded3.073s15392 KiB
48Time limit exceeded3.062s15264 KiB
49Time limit exceeded3.062s15372 KiB
50Time limit exceeded3.065s15252 KiB
51Time limit exceeded3.065s12744 KiB
52Time limit exceeded3.065s12784 KiB
53Time limit exceeded3.069s12812 KiB
54Time limit exceeded3.075s12872 KiB
55Time limit exceeded3.066s12848 KiB
56Time limit exceeded3.085s12896 KiB
subtask80/20
57Time limit exceeded3.078s5268 KiB
58Time limit exceeded3.062s6592 KiB
59Time limit exceeded3.042s8148 KiB
60Time limit exceeded3.078s10316 KiB
61Time limit exceeded3.065s14936 KiB
62Time limit exceeded3.073s15088 KiB
63Time limit exceeded3.051s15128 KiB
64Time limit exceeded3.055s15164 KiB
65Time limit exceeded3.049s15104 KiB
66Time limit exceeded3.062s15100 KiB
subtask90/25
67Time limit exceeded3.062s5176 KiB
68Time limit exceeded3.099s6648 KiB
69Time limit exceeded3.061s8148 KiB
70Time limit exceeded3.069s10560 KiB
71Time limit exceeded3.062s15040 KiB
72Time limit exceeded3.069s15396 KiB
73Time limit exceeded3.062s15428 KiB
74Time limit exceeded3.082s15480 KiB
75Time limit exceeded3.053s15472 KiB
76Time limit exceeded3.069s15288 KiB
77Time limit exceeded3.065s15172 KiB
78Time limit exceeded3.053s15364 KiB
79Time limit exceeded3.065s15288 KiB
80Time limit exceeded3.068s15532 KiB
81Time limit exceeded3.053s15564 KiB
82Time limit exceeded3.042s15092 KiB
83Time limit exceeded3.062s15492 KiB