Submission #690897

# Submission time Handle Problem Language Result Execution time Memory
690897 2023-01-30T15:16:57 Z PoonYaPat Synchronization (JOI13_synchronization) C++14
100 / 100
255 ms 23812 KB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;

int n,m,q,val[100001],level[100001],p[100001][18],line_val[100001],in[100001],out[100001],fen[100010],cnt;
pii line[200001];
bool active[100001];
vector<int> adj[100001];

void dfs(int x, int parent) {
    in[x]=++cnt;
    level[x]=level[parent]+1;
    p[x][0]=parent;
    for (int i=1; i<18; ++i) p[x][i]=p[p[x][i-1]][i-1];

    for (auto s : adj[x]) {
        if (s==parent) continue;
        dfs(s,x);
    }
    out[x]=cnt;
}

void update(int x, int val) {
    while (x<=n+5) {
        fen[x]+=val;
        x+=(x&-x);
    }
}

int find(int x) {
    int sum=0;
    while (x) {
        sum+=fen[x];
        x-=(x&-x);
    }
    return sum;
}

int f(int x) { //find leader of group
    int k=find(in[x]);
    for (int i=17; i>=0; --i) {
        if (p[x][i]!=0 && find(in[p[x][i]])==k) x=p[x][i];
    }
    return x;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n>>m>>q;
    for (int i=1; i<=n-1; ++i) {
        cin>>line[i].first>>line[i].second;
        adj[line[i].first].push_back(line[i].second);
        adj[line[i].second].push_back(line[i].first);
    }
    dfs(1,0);
    for (int i=1; i<=n; ++i) {
        val[i]=1;
        update(in[i],1);
        update(out[i]+1,-1);
    }

    while (m--) {
        int x; cin>>x;
        int a=line[x].first, b=line[x].second;
        if (level[a]>level[b]) swap(a,b);

        a=f(a);
        if (active[x]) {
            line_val[x]=val[b]=val[a];
            update(in[b],1);
            update(out[b]+1,-1);
        } else {
            val[a]=val[a]+val[b]-line_val[x];
            update(in[b],-1);
            update(out[b]+1,1);
        }
        active[x]=!active[x];
    }

    while (q--) {
        int x; cin>>x;
        cout<<val[f(x)]<<"\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2704 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2820 KB Output is correct
7 Correct 14 ms 4224 KB Output is correct
8 Correct 12 ms 4108 KB Output is correct
9 Correct 11 ms 4128 KB Output is correct
10 Correct 149 ms 18360 KB Output is correct
11 Correct 159 ms 18356 KB Output is correct
12 Correct 204 ms 23048 KB Output is correct
13 Correct 73 ms 18264 KB Output is correct
14 Correct 106 ms 17460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 18244 KB Output is correct
2 Correct 74 ms 18108 KB Output is correct
3 Correct 85 ms 20404 KB Output is correct
4 Correct 84 ms 20372 KB Output is correct
# 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 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2772 KB Output is correct
7 Correct 16 ms 4436 KB Output is correct
8 Correct 239 ms 20964 KB Output is correct
9 Correct 231 ms 20984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 239 ms 20880 KB Output is correct
2 Correct 132 ms 20868 KB Output is correct
3 Correct 129 ms 20940 KB Output is correct
4 Correct 129 ms 20996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 3 ms 2824 KB Output is correct
6 Correct 13 ms 4228 KB Output is correct
7 Correct 199 ms 19264 KB Output is correct
8 Correct 255 ms 23812 KB Output is correct
9 Correct 87 ms 19520 KB Output is correct
10 Correct 131 ms 18712 KB Output is correct
11 Correct 101 ms 21504 KB Output is correct
12 Correct 104 ms 21452 KB Output is correct
13 Correct 133 ms 23300 KB Output is correct