Submission #1041175

#TimeUsernameProblemLanguageResultExecution timeMemory
1041175TrinhKhanhDungSynchronization (JOI13_synchronization)C++14
100 / 100
145 ms23080 KiB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(998244353)

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

template<class T1, class T2>
    bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

struct fen{
    vector<int> val;
    int n;

    fen(int _n = 0){
        n = _n;
        val.resize(n + 3, 0);
    }

    void update(int p, int c){
        while(p <= n){
            val[p] += c;
            p += p & -p;
        }
    }

    int getVal(int p){
        int ans = 0;
        while(p > 0){
            ans += val[p];
            p -= p & -p;
        }
        return ans;
    }
} bit;

const int MAX = 1e5 + 10, LOG = 17;

int N, M, Q;
vector<int> adj[MAX];
vector<pair<int, int>> save;
int d[MAX][LOG], in[MAX], out[MAX], timer, sta[MAX], num[MAX];

void dfs(int u, int p){
    d[u][0] = p;
    for(int i=1; i<LOG; i++){
        d[u][i] = d[d[u][i - 1]][i - 1];
    }
    in[u] = ++timer;
    for(int v: adj[u]) if(v != p){
        dfs(v, u);
    }
    out[u] = timer;
}

int root(int u){
    int cur = bit.getVal(in[u]);
    for(int i=LOG-1; i>=0; i--) if(d[u][i] != 0){
        int v = d[u][i];
        if(bit.getVal(in[v]) == cur){
            u = v;
        }
    }
    return u;
}

void solve(){
    cin >> N >> M >> Q;
    for(int i=1; i<N; i++){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        save.push_back(make_pair(u, v));
    }
    dfs(1, 0);
    bit = fen(N);
    for(int i=1; i<=N; i++){
        bit.update(in[i], 1);
        bit.update(out[i] + 1, -1);
    }
    for(int i=1; i<=N; i++){
        num[i] = 1;
    }
    while(M--){
        int id; cin >> id;
        id--;
        int u = save[id].fi, v = save[id].se;
        if(d[u][0] == v){
            swap(u, v);
        }
        if(sta[id] != -1){
            bit.update(in[v], -1);
            bit.update(out[v] + 1, 1);
            int r = root(u);
            num[r] += num[v] - sta[id];
            sta[id] = -1;
        }
        else{
            bit.update(in[v], 1);
            bit.update(out[v] + 1, -1);
            int r = root(u);
            num[v] = sta[id] = num[r];
        }
    }
    while(Q--){
        int u; cin >> u;
        cout << num[root(u)] << '\n';
    }
}

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("deggraph.inp", "r", stdin);
//    freopen("deggraph.out", "w", stdout);

    int t = 1;
//    cin >> t;
    while(t--){
        solve();
    }

    return 0;
}
#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...