Submission #1097675

# Submission time Handle Problem Language Result Execution time Memory
1097675 2024-10-08T02:11:50 Z blackslex Synchronization (JOI13_synchronization) C++17
20 / 100
203 ms 24152 KB
#include<bits/stdc++.h>
#define lsb(x) (x &(-x))

using namespace std;
using pii = pair<int, int>;

const int N = 1e5 + 5, K = 19;
int n, m, q, x, y, t, tin[N], tout[N], par[N], dp[K][N], fwk[N], ans[N], lst[N];
vector<int> v[N];
bool f[N];

void upd (int idx, int val) {
    for (; idx < N; idx += lsb(idx)) fwk[idx] += val;
}

int qr (int idx) {
    int res = 0;
    for (; idx; idx -= lsb(idx)) res += fwk[idx];
    return res;
}

void dfs (int cur, int par) {
    tin[cur] = ++t; ::par[cur] = dp[0][cur] = par;
    for (auto &e: v[cur]) {
        if (par == e) continue;
        dfs(e, cur);
    }
    tout[cur] = t;
}

int lca (int a) {
    int res = a;
    for (int i = K - 1; i >= 0; i--) {
        if (dp[i][res] && qr(tin[dp[i][res]]) == qr(tin[a])) res = dp[i][res];
    }
    return res;
}

int main() {
    scanf("%d %d %d", &n, &m, &q);
    vector<pii> u(n - 1);
    for (auto &[x, y]: u) scanf("%d %d", &x, &y), v[x].emplace_back(y), v[y].emplace_back(x);
    dfs(1, 0);
    for (int i = 1; i < K; i++) {
        for (int j = 1; j <= n; j++) dp[i][j] = dp[i - 1][dp[i - 1][j]];
    }
    for (auto &[x, y]: u) if (::par[x] == y) swap(x, y);
    for (int i = 1; i <= n; i++) ans[i] = 1, upd(tin[i], -1), upd(tout[i], 1);
    while (m--) {
        scanf("%d", &x);
        auto [y, z] = u[--x];
        if (f[x]) {
            ans[z] = lst[z] = ans[lca(y)];
            upd(tin[z], -1); upd(tout[z], -1);
        } else {
            ans[lca(y)] += ans[z] - lst[z];
            upd(tin[z], 1); upd(tout[z], -1);
        }
        f[x] = !f[x];
    }
    while (q--) {
        scanf("%d", &x);
        printf("%d\n", ans[lca(x)]);
    }
}

Compilation message

synchronization.cpp: In function 'int main()':
synchronization.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |     scanf("%d %d %d", &n, &m, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:42:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |     for (auto &[x, y]: u) scanf("%d %d", &x, &y), v[x].emplace_back(y), v[y].emplace_back(x);
      |                           ~~~~~^~~~~~~~~~~~~~~~~
synchronization.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |         scanf("%d", &x);
      |         ~~~~~^~~~~~~~~~
synchronization.cpp:62:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |         scanf("%d", &x);
      |         ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10776 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Incorrect 1 ms 10748 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 52 ms 20972 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 10588 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 203 ms 24152 KB Output is correct
2 Correct 101 ms 24060 KB Output is correct
3 Correct 99 ms 24144 KB Output is correct
4 Correct 99 ms 24148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10584 KB Output is correct
2 Incorrect 1 ms 10584 KB Output isn't correct
3 Halted 0 ms 0 KB -