64102023-11-28 10:11:02GhostLegkisebb nem oszthatócpp17Time limit exceeded 10/1003.099s15700 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 || (out > 2 && out % 2 == 0)) {
                        correct = false;
                        break;
                    }
                    node = last[node];
                }
                else {
                    node = last[node];
                    continue;
                }
            }
            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
1Accepted3ms1816 KiB
subtask25/5
2Accepted3ms2044 KiB
3Accepted3ms2256 KiB
4Accepted6ms2468 KiB
5Accepted29ms2552 KiB
6Accepted71ms2564 KiB
7Accepted208ms2840 KiB
8Accepted328ms3364 KiB
9Accepted328ms3320 KiB
10Accepted330ms3268 KiB
11Accepted333ms3556 KiB
subtask35/5
12Accepted3ms3364 KiB
13Accepted3ms3620 KiB
14Accepted4ms3804 KiB
15Accepted28ms3940 KiB
16Accepted68ms4060 KiB
17Accepted202ms4088 KiB
18Accepted324ms4300 KiB
19Accepted326ms4384 KiB
20Accepted324ms4384 KiB
21Accepted323ms4356 KiB
subtask40/10
22Time limit exceeded3.059s4524 KiB
23Time limit exceeded3.066s5016 KiB
24Time limit exceeded3.065s9684 KiB
25Time limit exceeded3.055s13216 KiB
26Time limit exceeded3.082s14488 KiB
27Time limit exceeded3.062s14476 KiB
subtask50/10
28Time limit exceeded3.078s4536 KiB
29Time limit exceeded3.046s4796 KiB
30Time limit exceeded3.073s9988 KiB
31Time limit exceeded3.065s13876 KiB
32Time limit exceeded3.062s15108 KiB
33Time limit exceeded3.082s15124 KiB
subtask60/10
34Accepted328ms5236 KiB
35Time limit exceeded3.065s5396 KiB
36Time limit exceeded3.069s7104 KiB
37Time limit exceeded3.069s10452 KiB
38Time limit exceeded3.065s14176 KiB
39Time limit exceeded3.063s15600 KiB
40Time limit exceeded3.099s15448 KiB
41Time limit exceeded3.099s15244 KiB
subtask70/15
42Accepted326ms5264 KiB
43Time limit exceeded3.075s5456 KiB
44Time limit exceeded3.062s7176 KiB
45Time limit exceeded3.066s10568 KiB
46Time limit exceeded3.086s14492 KiB
47Time limit exceeded3.066s15592 KiB
48Time limit exceeded3.062s15668 KiB
49Time limit exceeded3.075s15692 KiB
50Time limit exceeded3.066s15452 KiB
51Time limit exceeded3.062s12968 KiB
52Time limit exceeded3.059s13112 KiB
53Time limit exceeded3.046s13104 KiB
54Time limit exceeded3.051s13028 KiB
55Time limit exceeded3.078s13116 KiB
56Time limit exceeded3.062s13100 KiB
subtask80/20
57Time limit exceeded3.069s5512 KiB
58Time limit exceeded3.071s6808 KiB
59Time limit exceeded3.062s8156 KiB
60Time limit exceeded3.062s10520 KiB
61Time limit exceeded3.078s15128 KiB
62Time limit exceeded3.082s15364 KiB
63Time limit exceeded3.073s15348 KiB
64Time limit exceeded3.059s15348 KiB
65Time limit exceeded3.059s15324 KiB
66Time limit exceeded3.079s15396 KiB
subtask90/25
67Time limit exceeded3.066s5416 KiB
68Time limit exceeded3.055s6808 KiB
69Time limit exceeded3.062s8348 KiB
70Time limit exceeded3.055s10788 KiB
71Time limit exceeded3.075s15392 KiB
72Time limit exceeded3.032s15648 KiB
73Time limit exceeded3.063s15604 KiB
74Time limit exceeded3.082s15564 KiB
75Time limit exceeded3.055s15464 KiB
76Time limit exceeded3.066s15576 KiB
77Time limit exceeded3.055s15516 KiB
78Time limit exceeded3.065s15684 KiB
79Time limit exceeded3.069s15504 KiB
80Time limit exceeded3.065s15700 KiB
81Time limit exceeded3.046s15664 KiB
82Time limit exceeded3.071s15328 KiB
83Time limit exceeded3.053s15632 KiB