답안 #338234

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
338234 2020-12-22T19:47:52 Z aryan12 Birthday gift (IZhO18_treearray) C++17
0 / 100
1 ms 376 KB
#include <bits/stdc++.h>
using namespace std;

//for 56 points

const long long N = 101;
vector<long long> g[N];
//long long tim = 0;
//long long Start[N], End[N];
long long DP[18][N], depth[N];

void dfs(long long node, long long par) {
    DP[0][node] = par;
   // Start[node] = ++tim;
    for(long long i = 0; i < g[node].size(); i++) {
        if(g[node][i] != par) {
            depth[g[node][i]] = depth[node] + 1;
            dfs(g[node][i], node);
        }
    }
    //End[node] = ++tim;
}

void DPTable(long long n) {
    for(long long j = 1; j < 18; j++) {
        for(long long i = 1; i <= n; i++) {
            if(DP[i][j - 1] == -1)
                DP[i][j] = -1;
            else
                DP[i][j] = DP[DP[i][j - 1]][j - 1];
            cout << "DP[" << i << "][" << j << "] = " << DP[i][j] << endl;
        }
    }
}

long long lca(long long a, long long b) {
    if(depth[a] < depth[b])
        swap(a, b);
    long long diff = depth[a] - depth[b];
    for(long long i = 17; i >= 0; i--) {
        if(diff >= (1 << i)) {
            diff -= (1 << i);
            a = DP[i][a];
        }
    }
    if(a == b)
        return a;
    for(long long i = 17; i >= 0; i--) {
        if(DP[i][a] != DP[i][b]) {
            a = DP[i][a];
            b = DP[i][b];
        }
    }
    return DP[0][a];
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long n, m, q;
    cin >> n >> m >> q;
    for(long long i = 0; i < n - 1; i++) {
        long long v, u;
        cin >> v >> u;
        g[v].push_back(u);
        g[u].push_back(v);
    }
    depth[1] = 0;
    dfs(1, -1);
    DPTable(n);
    long long a[m + 1];
    for(long long i = 1; i <= m; i++) {
        cin >> a[i];
    }
    for(long long i = 1; i <= q; i++) {
        long long command;
        cin >> command;
        if(command == 1) {
            long long idx, val;
            cin >> idx >> val;
            a[idx] = val;
        }
        else {
            long long l, r, v;
            cin >> l >> r >> v;
            long long f = 0;
            for(long long j = l; j < r; j++) {
                if(lca(a[j], a[j + 1]) == v) {
                    cout << j << " " << j + 1 << "\n";
                    f = 1;
                    break;
                }
                if(a[j] == v) {
                    cout << j << " " << j << "\n";
                    f = 1;
                    break;
                }
            }
            if(a[r] == v) {
                cout << r << " " << r << "\n";
                f = 1;
            }
            if(f == 0) {
                cout << "-1 -1" << "\n";
            }
        }
    }
}

Compilation message

treearray.cpp: In function 'void dfs(long long int, long long int)':
treearray.cpp:15:28: 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]
   15 |     for(long long i = 0; i < g[node].size(); i++) {
      |                          ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 376 KB Expected integer, but "DP[1][1]" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 376 KB Expected integer, but "DP[1][1]" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 376 KB Expected integer, but "DP[1][1]" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 376 KB Expected integer, but "DP[1][1]" found
2 Halted 0 ms 0 KB -