답안 #898195

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
898195 2024-01-04T11:24:14 Z vjudge1 Tourism (JOI23_tourism) C++17
0 / 100
5000 ms 28204 KB
#pragma GCC optimize("unroll-loops")
#pragma gcc optimize("Ofast")
#pragma GCC optimization("Ofast")
#pragma optimize(Ofast)
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define str string
#define fastio ios::sync_with_stdio(0), cin.tie(0);
#define fs first
#define ss second
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define len(x) x.size()

#define print(a)          \
    for (auto &x : a)     \
        cout << x << " "; \
    cout << endl;

#define printmp(a)    \
    for (auto &x : a) \
        cout << x.fs << " " << x.ss << endl;

const int mod = 998244353;

struct Lowest_Common_Ancestor
{
    vector<vector<int>> LCA;
    vector<int> Degree;

    void dfs(int u, int p, vector<vector<int>> &tree)
    {
        LCA[u][0] = p;
        for (auto &x : tree[u])
        {
            if (x != p)
            {
                Degree[x] = Degree[u] + 1;
                dfs(x, u, tree);
            }
        }
    }

    Lowest_Common_Ancestor(vector<vector<int>> &tree)
    {
        LCA.resize(tree.size(), vector<int>((int)(log2(tree.size())) + 1));
        Degree.resize(tree.size(), 1);

        dfs(1, -1, tree);

        for (int i = 1; i <= tree.size() - 1; i++)
            for (int j = 1; j < LCA[i].size(); j++)
                if (LCA[i][j - 1] == -1)
                    LCA[i][j] = -1;
                else
                    LCA[i][j] = LCA[LCA[i][j - 1]][j - 1];
    }

    void binary_shifting(int &X, int k)
    {
        for (int i = LCA[X].size(); i >= 0; i--)
        {
            if (k >= (1 << i))
            {
                X = LCA[X][i];
                k -= (1 << i);
            }
        }
    }

    int lca(int X, int Y)
    {
        if (Degree[X] < Degree[Y])
            swap(X, Y);
        if (Degree[X] != Degree[Y])
            binary_shifting(X, Degree[X] - Degree[Y]);
        int k = LCA[0].size() - 1;
        while (X != Y)
        {
            int l = 0, r = k;
            while (l < r)
            {
                int m = (l + r + 1) >> 1;
                if (LCA[X][m] == LCA[Y][m])
                    r = m - 1;
                else
                    l = m;
            }
            X = LCA[X][l];
            Y = LCA[Y][l];
            k = l;
        }
        return X;
    }
};

vector<bool> used;
int ans = 0;
vector<int> parent;

void dfs(int n, int p, vector<vector<int>> &tree){
    parent[n] = p;
    for(auto x : tree[n])
        if(x != p)
            dfs(x, n, tree);
}

void dfs(int &n){
    if(used[n])
        return;
    used[n] = 1;
    ans++;
    dfs(parent[n]);
}

void solve(){
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<int>> tree(n + 1);
    for(int i = 0; i < n - 1; i ++){
        int a, b;
        cin >> a >> b;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }
    parent.resize(n + 1);
    dfs(1, -1, tree);
    vector<int> c(m);
    for(int i = 0; i < m; i ++)
        cin >> c[i];
    Lowest_Common_Ancestor lca(tree);
    for(int i = 0; i < q; i ++){
        int l, r;
        cin >> l >> r;
        int p = c[l - 1];
        for(int j = l; j < r; j ++)
            p = lca.lca(p, c[j]);
        used.clear();
        used.resize(n + 1);
        used[p] = 1;
        ans = 1;
        for(int j = l - 1; j < r; j ++)
            dfs(c[j]);
        cout << ans << endl;
    }
}

signed main()
{
    fastio
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
        cout << endl;
    }
}

Compilation message

tourism.cpp:2: warning: ignoring '#pragma gcc optimize' [-Wunknown-pragmas]
    2 | #pragma gcc optimize("Ofast")
      | 
tourism.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    3 | #pragma GCC optimization("Ofast")
      | 
tourism.cpp:4: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
    4 | #pragma optimize(Ofast)
      | 
tourism.cpp: In constructor 'Lowest_Common_Ancestor::Lowest_Common_Ancestor(std::vector<std::vector<long long int> >&)':
tourism.cpp:53:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |         for (int i = 1; i <= tree.size() - 1; i++)
      |                         ~~^~~~~~~~~~~~~~~~~~
tourism.cpp:54:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |             for (int j = 1; j < LCA[i].size(); j++)
      |                             ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Runtime error 1 ms 604 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Runtime error 1 ms 604 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 6 ms 348 KB Output is correct
4 Execution timed out 5043 ms 17612 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Runtime error 43 ms 28204 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 7 ms 464 KB Output is correct
4 Execution timed out 5093 ms 16136 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Runtime error 1 ms 604 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -