답안 #153131

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
153131 2019-09-12T14:35:39 Z georgerapeanu 동기화 (JOI13_synchronization) C++11
0 / 100
369 ms 17784 KB
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

const int NMAX = 1e5;

int n,m,q;
pair<int,int> edge[NMAX + 5];
vector<int> graph[NMAX + 5];
int fa[NMAX + 5];

int fst[NMAX + 5];
int lst[NMAX + 5];
int len;

int aint[4 * NMAX + 5];
bool used[NMAX + 5];

int dp[NMAX + 5];
int taken_father[NMAX + 5];
int lin[NMAX + 5];

void predfs(int nod,int tata){
    fst[nod] = ++len;
    lin[len] = nod;
    fa[nod] = tata;

    for(auto it:graph[nod]){
        if(it == tata){
            continue;
        }
        predfs(it,nod);
    }

    lst[nod] = len;
}

void build(int nod,int st,int dr){
    if(st == dr){
        aint[nod] = lst[lin[st]];
        return ;
    }

    int mid = (st + dr) / 2;

    build(nod * 2,st,mid);
    build(nod * 2 + 1,mid + 1,dr);

    aint[nod] = max(aint[nod * 2],aint[nod * 2 + 1]);
}

int query(int nod,int st,int dr,int l,int r,int val){
    if(r < st || l > dr){
        return 0;
    }

    if(l <= st && dr <= r){
        if(st == dr){
            return st;
        }
        else if(aint[2 * nod + 1] >= val){
            return query(nod * 2 + 1,(st + dr) / 2 + 1,dr,l,r,val);
        }
        else{
            return query(nod * 2,st,(st + dr) / 2,l,r,val);
        }
    }

    int mid = (st + dr) / 2;
    return max(query(nod * 2,st,mid,l,r,val),query(nod * 2 + 1,mid + 1,dr,l,r,val));
}

void update(int nod,int st,int dr,int pos,int val){
    if(dr < pos || st > pos){
        return ;
    }

    if(st == dr){
        aint[nod] = val;
        return ;
    }

    int mid = (st + dr) / 2;

    update(nod * 2,st,mid,pos,val);
    update(nod * 2 + 1,mid + 1,dr,pos,val);

    aint[nod] = max(aint[nod * 2],aint[nod * 2 + 1]);
}

inline int get_root(int nod){
    return lin[query(1,1,n,1,fst[nod],lst[nod])];
}

int main(){

    scanf("%d %d %d",&n,&m,&q);

    for(int i = 1;i < n;i++){
        scanf("%d %d",&edge[i].first,&edge[i].second);
        graph[edge[i].first].push_back(edge[i].second);
        graph[edge[i].second].push_back(edge[i].first);
    }

    for(int i = 1;i <= n;i++){
        dp[i] = 1;
        taken_father[i] = 0;
    }

    predfs(1,0);
    build(1,1,n);

    while(m--){
        int x;
        scanf("%d",&x);

        int ch = (edge[x].first == fa[edge[x].second] ? edge[x].second:edge[x].first);
        int root = get_root(fa[ch]);

        if(used[x] == false){
            dp[root] += dp[ch] - taken_father[ch];
            used[x] = true;
            update(1,1,n,fst[ch],0);
        }
        else{
            used[x] = false;
            dp[ch] = taken_father[ch] = dp[root];
            update(1,1,n,fst[ch],lst[ch]);
        }
    }

    while(q--){
        int x;
        scanf("%d",&x);
        printf("%d\n",dp[get_root(x)]);
    }

    return 0;
}

Compilation message

synchronization.cpp: In function 'int main()':
synchronization.cpp:99:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d",&n,&m,&q);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:102:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&edge[i].first,&edge[i].second);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:117:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&x);
         ~~~~~^~~~~~~~~
synchronization.cpp:136:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&x);
         ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2808 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Incorrect 4 ms 2684 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 165 ms 14820 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 369 ms 17784 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2864 KB Output is correct
2 Incorrect 4 ms 2680 KB Output isn't correct
3 Halted 0 ms 0 KB -