64072023-11-28 10:06:40GhostLegkisebb nem oszthatócpp17Wrong answer 0/1003.101s15720 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 {
                    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
1Wrong answer3ms1812 KiB
subtask20/5
2Accepted3ms2004 KiB
3Wrong answer4ms2244 KiB
4Wrong answer6ms2248 KiB
5Wrong answer1.264s2248 KiB
6Wrong answer1.975s2260 KiB
7Time limit exceeded3.078s1864 KiB
8Time limit exceeded3.053s2876 KiB
9Time limit exceeded3.017s2996 KiB
10Wrong answer331ms3284 KiB
11Wrong answer333ms3240 KiB
subtask30/5
12Wrong answer3ms3220 KiB
13Wrong answer3ms3480 KiB
14Wrong answer4ms3656 KiB
15Wrong answer971ms3756 KiB
16Wrong answer1.103s3984 KiB
17Wrong answer1.238s4004 KiB
18Wrong answer1.299s4044 KiB
19Wrong answer894ms4176 KiB
20Wrong answer326ms4240 KiB
21Wrong answer324ms4208 KiB
subtask40/10
22Time limit exceeded3.078s4392 KiB
23Time limit exceeded3.059s4892 KiB
24Time limit exceeded3.078s9760 KiB
25Time limit exceeded3.03s13584 KiB
26Time limit exceeded3.082s14736 KiB
27Time limit exceeded3.071s14900 KiB
subtask50/10
28Time limit exceeded3.062s4932 KiB
29Time limit exceeded3.062s5060 KiB
30Time limit exceeded3.069s9956 KiB
31Time limit exceeded3.072s13872 KiB
32Time limit exceeded3.066s15060 KiB
33Time limit exceeded3.072s14944 KiB
subtask60/10
34Wrong answer333ms4764 KiB
35Time limit exceeded3.062s5092 KiB
36Time limit exceeded3.076s6736 KiB
37Time limit exceeded3.062s10204 KiB
38Time limit exceeded3.065s13848 KiB
39Time limit exceeded3.063s15068 KiB
40Time limit exceeded3.059s15156 KiB
41Time limit exceeded3.059s15048 KiB
subtask70/15
42Wrong answer328ms5180 KiB
43Time limit exceeded3.078s5252 KiB
44Time limit exceeded3.062s6852 KiB
45Time limit exceeded3.051s10360 KiB
46Time limit exceeded3.062s14276 KiB
47Time limit exceeded3.078s15544 KiB
48Time limit exceeded3.059s15344 KiB
49Time limit exceeded3.055s15360 KiB
50Time limit exceeded3.058s15360 KiB
51Time limit exceeded3.043s12804 KiB
52Time limit exceeded3.066s12816 KiB
53Time limit exceeded3.069s13080 KiB
54Time limit exceeded3.058s12812 KiB
55Time limit exceeded3.058s12740 KiB
56Time limit exceeded3.075s12912 KiB
subtask80/20
57Time limit exceeded3.062s5292 KiB
58Time limit exceeded3.055s6728 KiB
59Time limit exceeded3.046s8148 KiB
60Time limit exceeded3.065s10540 KiB
61Time limit exceeded3.084s15192 KiB
62Time limit exceeded3.058s15288 KiB
63Time limit exceeded3.059s15256 KiB
64Time limit exceeded3.101s15324 KiB
65Time limit exceeded3.062s15256 KiB
66Time limit exceeded3.069s15320 KiB
subtask90/25
67Time limit exceeded3.065s5460 KiB
68Time limit exceeded3.073s6888 KiB
69Time limit exceeded3.062s8344 KiB
70Time limit exceeded3.065s10732 KiB
71Time limit exceeded3.058s15320 KiB
72Time limit exceeded3.073s15612 KiB
73Time limit exceeded3.065s15700 KiB
74Time limit exceeded3.065s15640 KiB
75Time limit exceeded3.059s15516 KiB
76Time limit exceeded3.071s15520 KiB
77Time limit exceeded3.073s15692 KiB
78Time limit exceeded3.062s15584 KiB
79Time limit exceeded3.073s15588 KiB
80Time limit exceeded3.068s15720 KiB
81Time limit exceeded3.068s15652 KiB
82Time limit exceeded3.059s15264 KiB
83Time limit exceeded3.082s15512 KiB