답안 #274764

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
274764 2020-08-19T15:21:28 Z LifeHappen__ 동기화 (JOI13_synchronization) C++14
50 / 100
408 ms 39544 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define forinc(a,b,c) for(int a=b, _c=c; a<=_c; ++a)
#define fordec(a,b,c) for(int a=b, _c=c; a>=_c; --a)
#define forv(a,b) for(auto &a:b)
#define ii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define all(a) begin(a),end(a)
#define rall(a) rbegin(a),rend(a)

const int N=2e5+5;
int n,m,q;
ii edge[N];
vector<int> ad[N];
int tin[N],tout[N],id,it[N],a[N],last[N],dd[N];
int pd[N][22];

void upd(int i,int val){
    for(; i<=n; i+=i&-i) it[i]+=val;
}
int get(int i){
    int ans=0;
    for(; i; i-=i&-i) ans+=it[i];
    return ans;
}
void up(int u,int v,int val){
    upd(u,val); upd(v+1,-val);
}

void dfs(int u,int par=-1){
    tin[u]=++id;
    forinc(i,1,22) pd[u][i]=pd[pd[u][i-1]][i-1];
    a[u]=1;
    forv(v,ad[u]) if(v!=par){
        pd[v][0]=u;
        dfs(v,u);
    }
    tout[u]=id;
}
int root(int x){
    fordec(i,22,0) if(pd[x][i] && get(tin[pd[x][i]])==get(tin[x])) x=pd[x][i];
    return x;
}


int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>n>>m>>q;
    forinc(i,1,n-1){
        int u,v;
        cin>>u>>v;
        ad[u].pb(v); ad[v].pb(u);
        edge[i]={u,v};
    }
    dfs(1,0);
    forinc(i,1,n) up(tin[i],tout[i],1);
    while(m--){
        int x;
        cin>>x;
        int u,v; tie(u,v)=edge[x];
        if(pd[u][0]==v) swap(u,v);
        if(!dd[x]){
            a[root(u)]+=a[v]-last[v];
            up(tin[v],tout[v],-1);
        }else{
            a[v]=last[v]=a[root(u)];
            up(tin[v],tout[v],1);
        }
        dd[x]=!dd[x];
    }
    while(q--){
        int x;
        cin>>x;
        cout<<a[root(x)]<<'\n';
    }
}

Compilation message

synchronization.cpp: In function 'void dfs(long long int, long long int)':
synchronization.cpp:37:28: warning: iteration 21 invokes undefined behavior [-Waggressive-loop-optimizations]
   37 |     forinc(i,1,22) pd[u][i]=pd[pd[u][i-1]][i-1];
      |                    ~~~~~~~~^~~~~~~~~~~~~~~~~~~~
synchronization.cpp:6:43: note: within this loop
    6 | #define forinc(a,b,c) for(int a=b, _c=c; a<=_c; ++a)
      |                                           ^
synchronization.cpp:37:5: note: in expansion of macro 'forinc'
   37 |     forinc(i,1,22) pd[u][i]=pd[pd[u][i-1]][i-1];
      |     ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Incorrect 4 ms 5120 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 122 ms 35828 KB Output is correct
2 Correct 118 ms 35648 KB Output is correct
3 Correct 156 ms 37368 KB Output is correct
4 Correct 157 ms 37372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 5 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 5 ms 5376 KB Output is correct
7 Correct 26 ms 8448 KB Output is correct
8 Correct 396 ms 39544 KB Output is correct
9 Correct 381 ms 39544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 408 ms 39504 KB Output is correct
2 Correct 238 ms 38396 KB Output is correct
3 Correct 240 ms 38648 KB Output is correct
4 Correct 240 ms 38776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Incorrect 3 ms 5120 KB Output isn't correct
3 Halted 0 ms 0 KB -