64062023-11-28 10:02:05GhostLegkisebb nem oszthatócpp17Wrong answer 0/1003.084s15784 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 {
                    break;
                }
            }
            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 answer3ms1944 KiB
subtask20/5
2Wrong answer3ms2112 KiB
3Wrong answer3ms2332 KiB
4Wrong answer6ms2384 KiB
5Wrong answer731ms2728 KiB
6Wrong answer842ms2828 KiB
7Wrong answer906ms3056 KiB
8Wrong answer1.083s3156 KiB
9Wrong answer1.024s3408 KiB
10Wrong answer330ms3620 KiB
11Wrong answer328ms3888 KiB
subtask30/5
12Wrong answer3ms3680 KiB
13Wrong answer3ms3684 KiB
14Wrong answer4ms3700 KiB
15Wrong answer814ms3924 KiB
16Wrong answer778ms4260 KiB
17Wrong answer939ms4172 KiB
18Wrong answer1.046s4116 KiB
19Wrong answer1.027s4372 KiB
20Wrong answer326ms4380 KiB
21Wrong answer326ms4332 KiB
subtask40/10
22Time limit exceeded3.073s4604 KiB
23Time limit exceeded3.046s4708 KiB
24Time limit exceeded3.066s9508 KiB
25Time limit exceeded3.062s13132 KiB
26Time limit exceeded3.059s14384 KiB
27Time limit exceeded3.053s14452 KiB
subtask50/10
28Time limit exceeded3.078s4596 KiB
29Time limit exceeded3.035s4964 KiB
30Time limit exceeded3.062s9900 KiB
31Time limit exceeded3.049s13700 KiB
32Time limit exceeded3.066s14804 KiB
33Time limit exceeded3.068s14988 KiB
subtask60/10
34Wrong answer330ms5084 KiB
35Time limit exceeded3.062s5056 KiB
36Time limit exceeded3.073s6576 KiB
37Time limit exceeded3.049s9944 KiB
38Time limit exceeded3.073s13796 KiB
39Time limit exceeded3.079s14964 KiB
40Time limit exceeded3.062s15120 KiB
41Time limit exceeded3.078s15076 KiB
subtask70/15
42Wrong answer328ms5048 KiB
43Time limit exceeded3.026s5140 KiB
44Time limit exceeded3.039s6824 KiB
45Time limit exceeded3.069s10256 KiB
46Time limit exceeded3.072s14132 KiB
47Time limit exceeded3.084s15292 KiB
48Time limit exceeded3.075s15252 KiB
49Time limit exceeded3.075s15212 KiB
50Time limit exceeded3.073s15372 KiB
51Time limit exceeded3.058s12688 KiB
52Time limit exceeded3.072s12784 KiB
53Time limit exceeded3.062s12804 KiB
54Time limit exceeded3.058s12776 KiB
55Time limit exceeded3.079s12704 KiB
56Time limit exceeded3.062s12956 KiB
subtask80/20
57Time limit exceeded3.073s5376 KiB
58Time limit exceeded3.078s6640 KiB
59Time limit exceeded3.082s8156 KiB
60Time limit exceeded3.069s10452 KiB
61Time limit exceeded3.059s14848 KiB
62Time limit exceeded3.079s14960 KiB
63Time limit exceeded3.079s15012 KiB
64Time limit exceeded3.066s15060 KiB
65Time limit exceeded3.072s15032 KiB
66Time limit exceeded3.071s15188 KiB
subtask90/25
67Time limit exceeded3.049s5236 KiB
68Time limit exceeded3.075s6640 KiB
69Time limit exceeded3.046s7968 KiB
70Time limit exceeded3.078s10528 KiB
71Time limit exceeded3.066s15248 KiB
72Time limit exceeded3.082s15340 KiB
73Time limit exceeded3.078s15392 KiB
74Time limit exceeded3.059s15336 KiB
75Time limit exceeded3.062s15276 KiB
76Time limit exceeded3.069s15416 KiB
77Time limit exceeded3.071s15700 KiB
78Time limit exceeded3.075s15488 KiB
79Time limit exceeded3.082s15532 KiB
80Time limit exceeded3.071s15784 KiB
81Time limit exceeded3.058s15608 KiB
82Time limit exceeded3.073s15208 KiB
83Time limit exceeded3.075s15476 KiB