제출 #1269051

#제출 시각아이디문제언어결과실행 시간메모리
1269051Bui_Quoc_CuongTourism (JOI23_tourism)C++20
28 / 100
5094 ms14788 KiB
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, m, q;
vector <int> g[maxn];
int c[maxn];
int par[maxn][18], h[maxn], tin[maxn], timedfs;
void dfs(int u){
    tin[u] = ++ timedfs;
    for(int &v : g[u]) if(v != par[u][0]){
        par[v][0] = u;
        h[v] = h[u] + 1;
        for(int j = 1; (1 << j) <= n; j++){
            par[v][j] = par[par[v][j - 1]][j - 1];
        }
        dfs(v);
    }
}
int LCA(int u, int v){
    if(h[u] < h[v]) swap(u, v);
    int z = __lg(h[u]);
    for(int s = z; s >= 0; s--) if(h[u] - h[v] >= (1 << s)) u = par[u][s];
    if(u == v) return u;
    for(int s = z; s >= 0; s--) if(par[u][s] != par[v][s]) u = par[u][s], v = par[v][s];
    return par[u][0];
}
int dist(int u, int v){
    return h[u] + h[v] - 2 * h[LCA(u, v)];
}
namespace sub1{

    void solve(){
        while(q--){
            int L, R; cin >> L >> R;
            vector <int> tour;
            for(int i = L; i <= R; i++){
                tour.push_back(c[i]);
            }
            sort(tour.begin(), tour.end(), [&](int u, int v){
                return tin[u] < tin[v];
            });
            tour.resize(unique(tour.begin(), tour.end()) - tour.begin());
            int ans = 0;
            for(int i = 1; i < (int)tour.size(); i++){
                ans+= dist(tour[i - 1], tour[i]);
            }
            ans+= dist(tour.back(), tour[0]);
            cout << ans / 2 + 1 << "\n";
        }
    }
}
int32_t main(){
    ios_base::sync_with_stdio(0); cin.tie(0); 
    #define koa "kieuoanh"
    if(fopen(koa".inp", "r")){
        freopen(koa".inp", "r", stdin); freopen(koa".out", "w", stdout);
    }
    cin >> n >> m >> q;
    for(int i = 1; i < n; i++){
        int u, v; cin >> u >> v;
        g[u].push_back(v); g[v].push_back(u);
    }
    for(int i = 1; i <= m; i++){
        cin >> c[i];
    }
    dfs(1);
    sub1::solve();
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

tourism.cpp: In function 'int32_t main()':
tourism.cpp:56:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |         freopen(koa".inp", "r", stdin); freopen(koa".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:56:48: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |         freopen(koa".inp", "r", stdin); freopen(koa".out", "w", stdout);
      |                                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...