46312023-03-30 13:12:59gortomiLegkisebb nem oszthatócpp17Wrong answer 45/1001.504s42072 KiB
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g;
int tim = 0;
vector<int> l, r;
void dfs(int n, int p)
{
    l[n] = tim++;
    for(auto x : g[n]) if(x != p) dfs(x, n);
    r[n] = tim++;
}
struct val
{
    int l, r, ind;
};
bool comp(val a, val b)
{
    return a.l / 350 < b.l / 350 || (a.l / 350 == b.l / 350 && a.r < b.r);
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> prim;
    vector<bool> ip(1e6 + 1, 1);
    vector<int> pl(1e6 + 1);
    g.resize(n + 1);
    l.resize(n + 1);
    r.resize(n + 1);
    for(int i = 2; i <= 1e6; i++)
    {
        if(ip[i])
        {
            pl[i] = prim.size();
            prim.push_back(i);
            for(int j = 2 * i; j <= 1e6; j += i)
            {
                ip[j] = 0;
            }
        }
    }
    vector<int> v(n + 1, 1e6 + 1);
    for(int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        if(ip[x]) v[i] = pl[x];
    }
    for(int i = 0; i < n - 1; i++)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1, 0);
    vector<int> path(2 * n);
    for(int i = 1; i <= n; i++)
    {
        path[l[i]] = i;
        path[r[i]] = i;
    }
    int q;
    cin >> q;
    vector<val> que(q + 1);
    que[0] = {0, -1, 0};
    vector<int> ans(q);
    for(int i = 1; i <= q; i++)
    {
        int x, y;
        cin >> x >> y;
        if(l[x] < l[y] && r[x] < r[y])
        {
            que[i].l = r[x];
            que[i].r = l[y];
        }
        else if(l[x] > l[y] && r[x] > r[y])
        {
            que[i].l = r[y];
            que[i].r = l[x];
        }
        else
        {
            que[i].l = l[x];
            que[i].r = l[y];
            if(que[i].l > que[i].r) swap(que[i].l, que[i].r);
        }
        que[i].ind = i - 1;
    }
    sort(que.begin(), que.end(), comp);
    const int b = 350;
    vector<int> sq(b);
    vector<int> db(n + 1);
    vector<bool> act(n + 1);
    for(int i = 1; i <= q; i++)
    {
        for(int j = que[i - 1].l - 1; j >= que[i].l; j--)
        {
            int y = path[j];
            int x = v[y];
            if(x > n) continue;
            if(!act[y])
            {
                act[y] = 1;
                db[x]++;
                if(db[x] == 1) sq[x / b]++;
            }
            else
            {
                act[y] = 0;
                db[x]--;
                if(db[x] == 0) sq[x / b]--;
            }
        }
        for(int j = que[i - 1].r + 1; j <= que[i].r; j++)
        {
            int y = path[j];
            int x = v[y];
            if(x > n) continue;
            if(!act[y])
            {
                act[y] = 1;
                db[x]++;
                if(db[x] == 1) sq[x / b]++;
            }
            else
            {
                act[y] = 0;
                db[x]--;
                if(db[x] == 0) sq[x / b]--;
            }
        }
        for(int j = que[i - 1].l; j <= que[i].l - 1; j++)
        {
            int y = path[j];
            int x = v[y];
            if(x > n) continue;
            if(!act[y])
            {
                act[y] = 1;
                db[x]++;
                if(db[x] == 1) sq[x / b]++;
            }
            else
            {
                act[y] = 0;
                db[x]--;
                if(db[x] == 0) sq[x / b]--;
            }
        }

        for(int j = que[i - 1].r; j >= que[i].r + 1; j--)
        {
            int y = path[j];
            int x = v[y];
            if(x > n) continue;
            if(!act[y])
            {
                act[y] = 1;
                db[x]++;
                if(db[x] == 1) sq[x / b]++;
            }
            else
            {
                act[y] = 0;
                db[x]--;
                if(db[x] == 0) sq[x / b]--;
            }
        }
        for(int j = 0; j < b; j++)
        {
            if(sq[j] < b)
            {
                for(int k = 0; k < b; k++)
                {
                    if(db[j * b + k] == 0)
                    {
                        ans[que[i].ind] = j * b + k;
                        break;
                    }
                }
                break;
            }
        }
    }
    for(auto x : ans) cout << prim[x] << "\n";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted18ms10948 KiB
subtask25/5
2Accepted17ms11164 KiB
3Accepted18ms11384 KiB
4Accepted17ms11352 KiB
5Accepted17ms11688 KiB
6Accepted18ms11992 KiB
7Accepted18ms12088 KiB
8Accepted18ms12308 KiB
9Accepted18ms12548 KiB
10Accepted18ms12492 KiB
11Accepted19ms12496 KiB
subtask30/5
12Accepted17ms12336 KiB
13Accepted17ms12696 KiB
14Wrong answer17ms12796 KiB
15Accepted18ms12944 KiB
16Accepted18ms12800 KiB
17Accepted18ms13284 KiB
18Accepted19ms13240 KiB
19Accepted18ms13204 KiB
20Wrong answer19ms13332 KiB
21Wrong answer19ms13212 KiB
subtask410/10
22Accepted35ms14696 KiB
23Accepted136ms18652 KiB
24Accepted203ms27408 KiB
25Accepted294ms34148 KiB
26Accepted330ms38220 KiB
27Accepted330ms37760 KiB
subtask50/10
28Wrong answer46ms14940 KiB
29Wrong answer165ms18036 KiB
30Wrong answer569ms25076 KiB
31Wrong answer1.19s30228 KiB
32Wrong answer1.496s32192 KiB
33Wrong answer1.504s32196 KiB
subtask610/10
34Accepted18ms13976 KiB
35Accepted32ms15944 KiB
36Accepted79ms20048 KiB
37Accepted164ms28272 KiB
38Accepted232ms36556 KiB
39Accepted266ms37280 KiB
40Accepted266ms40548 KiB
41Accepted275ms37412 KiB
subtask70/15
42Accepted18ms13992 KiB
43Accepted35ms15168 KiB
44Accepted97ms18896 KiB
45Accepted252ms25208 KiB
46Accepted455ms30452 KiB
47Accepted547ms32228 KiB
48Accepted544ms32500 KiB
49Accepted537ms32392 KiB
50Wrong answer584ms32452 KiB
51Wrong answer119ms36588 KiB
52Accepted152ms29248 KiB
53Accepted155ms35272 KiB
54Wrong answer119ms36472 KiB
55Accepted152ms29120 KiB
56Accepted1.1s29176 KiB
subtask820/20
57Accepted34ms15940 KiB
58Accepted78ms20764 KiB
59Accepted150ms24768 KiB
60Accepted256ms30432 KiB
61Accepted257ms39264 KiB
62Accepted261ms41424 KiB
63Accepted379ms37912 KiB
64Accepted333ms38760 KiB
65Accepted173ms42072 KiB
66Accepted166ms41984 KiB
subtask90/25
67Wrong answer41ms15584 KiB
68Accepted90ms18684 KiB
69Wrong answer194ms22216 KiB
70Wrong answer620ms26032 KiB
71Accepted523ms32484 KiB
72Accepted542ms32640 KiB
73Wrong answer1.148s32616 KiB
74Wrong answer810ms32628 KiB
75Wrong answer669ms32676 KiB
76Wrong answer828ms32624 KiB
77Wrong answer892ms32752 KiB
78Wrong answer736ms32732 KiB
79Wrong answer903ms32624 KiB
80Accepted367ms36284 KiB
81Accepted398ms36284 KiB
82Accepted175ms41636 KiB
83Accepted833ms37732 KiB