답안 #804387

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
804387 2023-08-03T08:28:02 Z mousebeaver Tourism (JOI23_tourism) C++14
28 / 100
5000 ms 25940 KB
#define ll long long
#define INF numeric_limits<ll>::max()
#include <bits/stdc++.h>
using namespace std;

void init(ll i, vector<ll>& depth, vector<vector<ll>>& adjlist, vector<bool>& visited, vector<ll>& subtree, vector<vector<ll>>& jump)
{
    visited[i] = true;

    for(ll j : adjlist[i])
    {
        if(visited[j])
        {
            jump[0][i] = j;
            depth[i] = depth[j]+1;
        }
    }
    for(ll j : adjlist[i])
    {
        if(!visited[j])
        {
            init(j, depth, adjlist, visited, subtree, jump);
            subtree[i] += subtree[j];
        }
    }
}

ll lift(ll a, vector<vector<ll>>& jump, ll dist)
{
    for(ll i = jump.size()-1; i >= 0 && a != -1; i--)
    {
        if(((1<<i)&dist) != 0)
        {
            a = jump[i][a];
        }
    }
    return a;
}

ll lca(ll a, ll b, vector<vector<ll>>& jump, vector<ll>& depth)
{
    if(depth[a] < depth[b])
        swap(a, b);
    a = lift(a, jump, depth[a]-depth[b]);

    if(a == b)
    {
        return a;
    }

    for(ll j = jump.size()-1; j >= 0; j--)
    {
        if(jump[j][a] != jump[j][b])
        {
            a = jump[j][a];
            b = jump[j][b];
        }
    }
    return jump[0][a];
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    ll n, m, q;
    cin>>n>>m>>q;
    vector<vector<ll>> adjlist(n, vector<ll> (0));
    for(ll i = 0; i < n-1; i++)
    {
        ll a, b;
        cin>>a>>b;
        a--; b--;
        adjlist[a].push_back(b);
        adjlist[b].push_back(a);
    }

    ll segnodes = 1;
    ll logdepth = 0;
    while(2*n > segnodes)
    {
        segnodes *= 2;
        logdepth++;
    }
    vector<vector<ll>> jump(logdepth, vector<ll> (n, -1));

    vector<ll> subtree(n, 1);
    vector<bool> visited(n, false);
    vector<ll> depth(n, 0);
    init(0, depth, adjlist, visited, subtree, jump);

    for(ll i = 1; i < logdepth; i++)
    {
        for(ll j = 0; j < n; j++)
        {
            if(jump[i-1][j] != -1)
            {
                jump[i][j] = jump[i-1][jump[i-1][j]];
            }
        }
    }

    vector<ll> c(m);
    for(ll i = 0; i < m; i++)
    {
        cin>>c[i];
        c[i]--;
    }

    for(ll x = 0; x < q; x++)
    {
        ll left, right;
        cin>>left>>right;
        left--; right--;

        if(left == right)
        {
            cout<<1<<"\n";
            continue;
        }

        ll l = lca(c[left], c[left+1], jump, depth);
        for(ll i = left+2; i <= right; i++)
        {
            l = lca(l, c[i], jump, depth);
        }

        visited.assign(n, false);
        visited[l] = true;
        ll counter = 1;
        for(ll i = left; i <= right; i++)
        {
            ll node = c[i];
            while(!visited[node])
            {
                visited[node] = true;
                counter++;
                node = jump[0][node];
            }
        }
        cout<<counter<<"\n";
    }


    return 0;  
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 2 ms 324 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 320 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 2 ms 324 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 320 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 22 ms 600 KB Output is correct
31 Correct 31 ms 612 KB Output is correct
32 Correct 46 ms 724 KB Output is correct
33 Correct 55 ms 724 KB Output is correct
34 Correct 52 ms 704 KB Output is correct
35 Correct 104 ms 724 KB Output is correct
36 Correct 116 ms 700 KB Output is correct
37 Correct 105 ms 708 KB Output is correct
38 Correct 76 ms 824 KB Output is correct
39 Correct 75 ms 824 KB Output is correct
40 Correct 75 ms 832 KB Output is correct
41 Correct 236 ms 856 KB Output is correct
42 Correct 263 ms 864 KB Output is correct
43 Correct 234 ms 804 KB Output is correct
44 Correct 73 ms 784 KB Output is correct
45 Correct 69 ms 744 KB Output is correct
46 Correct 71 ms 784 KB Output is correct
47 Correct 199 ms 744 KB Output is correct
48 Correct 208 ms 736 KB Output is correct
49 Correct 202 ms 756 KB Output is correct
50 Correct 21 ms 724 KB Output is correct
51 Correct 24 ms 724 KB Output is correct
52 Correct 22 ms 724 KB Output is correct
53 Correct 22 ms 724 KB Output is correct
54 Correct 26 ms 716 KB Output is correct
55 Correct 22 ms 728 KB Output is correct
56 Correct 8 ms 368 KB Output is correct
57 Correct 1 ms 688 KB Output is correct
58 Correct 1 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 324 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 8 ms 332 KB Output is correct
4 Execution timed out 5036 ms 17784 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 39 ms 12396 KB Output is correct
3 Correct 45 ms 14732 KB Output is correct
4 Correct 40 ms 16700 KB Output is correct
5 Correct 54 ms 24636 KB Output is correct
6 Correct 52 ms 24640 KB Output is correct
7 Correct 62 ms 24640 KB Output is correct
8 Correct 55 ms 24612 KB Output is correct
9 Correct 58 ms 24568 KB Output is correct
10 Correct 57 ms 24540 KB Output is correct
11 Correct 59 ms 24536 KB Output is correct
12 Correct 62 ms 24636 KB Output is correct
13 Correct 75 ms 24924 KB Output is correct
14 Correct 81 ms 24964 KB Output is correct
15 Correct 70 ms 25940 KB Output is correct
16 Correct 75 ms 24768 KB Output is correct
17 Correct 69 ms 24712 KB Output is correct
18 Correct 65 ms 24672 KB Output is correct
19 Correct 57 ms 24124 KB Output is correct
20 Correct 59 ms 24168 KB Output is correct
21 Correct 56 ms 24100 KB Output is correct
22 Correct 60 ms 24124 KB Output is correct
23 Correct 69 ms 24216 KB Output is correct
24 Correct 86 ms 24124 KB Output is correct
25 Correct 112 ms 24124 KB Output is correct
26 Correct 137 ms 24260 KB Output is correct
27 Correct 192 ms 24240 KB Output is correct
28 Correct 242 ms 24316 KB Output is correct
29 Correct 295 ms 24252 KB Output is correct
30 Correct 373 ms 24452 KB Output is correct
31 Correct 396 ms 24368 KB Output is correct
32 Correct 477 ms 24632 KB Output is correct
33 Correct 358 ms 24920 KB Output is correct
34 Correct 61 ms 25620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 9 ms 328 KB Output is correct
4 Execution timed out 5042 ms 13184 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 2 ms 324 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 320 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 22 ms 600 KB Output is correct
31 Correct 31 ms 612 KB Output is correct
32 Correct 46 ms 724 KB Output is correct
33 Correct 55 ms 724 KB Output is correct
34 Correct 52 ms 704 KB Output is correct
35 Correct 104 ms 724 KB Output is correct
36 Correct 116 ms 700 KB Output is correct
37 Correct 105 ms 708 KB Output is correct
38 Correct 76 ms 824 KB Output is correct
39 Correct 75 ms 824 KB Output is correct
40 Correct 75 ms 832 KB Output is correct
41 Correct 236 ms 856 KB Output is correct
42 Correct 263 ms 864 KB Output is correct
43 Correct 234 ms 804 KB Output is correct
44 Correct 73 ms 784 KB Output is correct
45 Correct 69 ms 744 KB Output is correct
46 Correct 71 ms 784 KB Output is correct
47 Correct 199 ms 744 KB Output is correct
48 Correct 208 ms 736 KB Output is correct
49 Correct 202 ms 756 KB Output is correct
50 Correct 21 ms 724 KB Output is correct
51 Correct 24 ms 724 KB Output is correct
52 Correct 22 ms 724 KB Output is correct
53 Correct 22 ms 724 KB Output is correct
54 Correct 26 ms 716 KB Output is correct
55 Correct 22 ms 728 KB Output is correct
56 Correct 8 ms 368 KB Output is correct
57 Correct 1 ms 688 KB Output is correct
58 Correct 1 ms 596 KB Output is correct
59 Correct 0 ms 324 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 8 ms 332 KB Output is correct
62 Execution timed out 5036 ms 17784 KB Time limit exceeded
63 Halted 0 ms 0 KB -