Submission #577765

# Submission time Handle Problem Language Result Execution time Memory
577765 2022-06-15T08:45:08 Z Do_you_copy Synchronization (JOI13_synchronization) C++14
0 / 100
185 ms 26896 KB
#include <bits/stdc++.h>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;

using ll = long long;
using pii = pair <int, int>;

ll min(const ll &a, const ll &b){
    return (a < b) ? a : b;
}

ll max(const ll &a, const ll &b){
    return (a > b) ? a : b;
}

const ll Mod = 1000000007;
const int maxN = 1e5 + 1;
int n, m, q, cnt, k;
int lift[maxN][18];
int child[maxN];
bool open[maxN];
int newidx[maxN];
int last[maxN];
int ans[maxN];

struct TEdge{
    int u, v;
};

int a[maxN + 1];
TEdge e[maxN];

vector <int> adj[maxN];

int lastval;
void dfs(int u, int p, int deg){
    a[++cnt] = deg - lastval;
    newidx[u] = cnt;
    lastval = deg;
    lift[u][0] = max(1, p);
    for (int i = 1; i <= k; ++i){
        lift[u][i] = max(lift[lift[u][i - 1]][i - 1], 1);
    }
    for (const int &i: adj[u]){
        if (i != p){
            dfs(i, u, deg + 1);
            child[u] += child[i] + 1;
        }
    }
}

int bit[maxN];

void update(int i, int v){
    //binary indexed tree
    while (i <= n){
        bit[i] += v;
        i += i & (-i);
    }
}

int kid(int u, int v){
    return (child[u] > child[v]) ? v : u;
}

int get(int i){
    int tem = 0;
    while (i > 0){
        tem += bit[i];
        i -= i & (-i);
    }
    return tem;
}

int findancestor(int u){
    for (int i = k; i >= 0; --i){
        if (get(newidx[lift[u][i]]) == get(newidx[u])){
            u = lift[u][i];
        }
    }
    return u;
}

void Init(){
    cin >> n >> m >> q;
    fill (ans + 1, ans + n + 1, 1);
    k = log2(n) + 1;
    for (int i = 1; i < n; ++i){
        cin >> e[i].u >> e[i].v;
        adj[e[i].u].pb(e[i].v);
        adj[e[i].v].pb(e[i].u);
    }
    dfs(1, 0, 1);
    for (int i = 1; i <= n; ++i) update(i, a[i]);
    while (m--){
        int i; cin >> i;
        open[i] = !open[i];
        int v = kid(e[i].u, e[i].v);
        int p = findancestor(v);
        int l = newidx[v];
        int r = l + child[v];
        if (open[i]){
            update(l, -1);
            update(r + 1, 1);
            ans[v ^ e[i].v ^ e[i].u] += ans[v] - last[v];
        }
        else{
            update(l, 1);
            update(r + 1, -1);
            last[v] = ans[v] = ans[v ^ e[i].v ^ e[i].u];
        }
    }
    while (q--){
        int x; cin >> x; cout << ans[findancestor(x)] << "\n";
    }
}

int main(){
    if (fopen(taskname".txt", "r")){
        freopen(taskname".txt", "r", stdin);
    }
    faster
    //freopen(taskname.inp, "r", stdin)
    //freopen(taskname.out, "w", stdout)
    Init();
}

Compilation message

synchronization.cpp: In function 'void Init()':
synchronization.cpp:103:13: warning: unused variable 'p' [-Wunused-variable]
  103 |         int p = findancestor(v);
      |             ^
synchronization.cpp: In function 'int main()':
synchronization.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |         freopen(taskname".txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Incorrect 2 ms 2644 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 68 ms 21712 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2688 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 185 ms 26896 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Incorrect 2 ms 2680 KB Output isn't correct
3 Halted 0 ms 0 KB -