답안 #290094

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
290094 2020-09-03T12:04:09 Z model_code Spring cleaning (CEOI20_cleaning) C++17
100 / 100
261 ms 32372 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

/*
LCA solution, it works when you don't know the ith question before answering the i-1th
Note that this is an O(NlogN) solution

Made for you by Mate Busa
*/

int N, Q, a, b, cur, num, o, ans, root, dif, last, level, oo;
vector<int> neighbour[200000];
bool visited[200000];
int dis[200000];
int parent[200000];
int order[200000][2];
int number[200000];
int dp[200000];
int fia[200000];
int f[200000];
vector<int> d[200000];
set<int> reuse;
set<int> who;
vector<int> vec;

void dfs(int x){
    visited[x] = true; order[x][0] = cur++; oo++;
    if(root==x){
        parent[x] = -1;
    } else {
        int hat = 0; while((1<<hat)<=dis[x]) hat++;
        d[x].resize(hat); d[x][0] = parent[x];
        for(int i=1; i<hat; i++) d[x][i] = d[d[x][i-1]][i-1];
    }
    bool lv = true;
    for(int i:neighbour[x]){
        if(!visited[i]){
            dis[i] = dis[x]+1; parent[i] = x;
            dfs(i); lv = false; dp[x] += dp[i];
        }
    }
    if(lv) dp[x]++;
    if(dp[x]%2==0) o++;
    order[x][1] = cur++;
}
void pre(int x){
    for(int i:neighbour[x]){
        if(parent[x]!=i){
            dp[i] = dp[i]%2 + dp[x];
            pre(i);
        }
    }
}
int lca(int x, int y){
    if(dis[y]<dis[x]) swap(x,y);
    for(int i=d[y].size()-1; dis[x]!=dis[y]; i--){
        if(dis[y]-(1<<i)>=dis[x]) y = d[y][i];
    }
    if(x==y) return x;
    for(int i=d[x].size()-1; parent[x]!=parent[y]; i--){
        if((1<<i)<=dis[x] && d[x][i]!=d[y][i]){x = d[x][i]; y = d[y][i];}
    }
    return parent[x];
}

bool smaller(int x, int y){
    return order[x][dif] < order[y][dif];
}

int calc(int x){
    return (dp[x]-(dis[x]+1-dp[x]));
}
///find parent in LCA tree
void up(){
    if(number[a]%2==1){
        ans += calc(a);
        if(f[a]!=-1)  ans -= calc(f[a]);
    }
    if(f[a]!=-1) number[f[a]] += number[a];
    a = f[a];
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr); std::cout.tie(nullptr);

    cin >> N >> Q;
    for(int i=0; i<N-1; i++){
        cin >> a >> b;
        neighbour[a-1].push_back(b-1);
        neighbour[b-1].push_back(a-1);
    }
    ///find an inside point
    for(int i=0; i<N; i++) if(neighbour[i].size()!=1) {root = i; break;}

    dfs(root); level = dp[root];
    dp[root] %= 2; pre(root);

    for(int ii=0; ii<Q; ii++){
        reuse.clear(); vec.clear(); who.clear(); ans = N-1+o;
        cin >> num; ans += num;
        for(int i=0; i<num; i++){
            cin >> a; number[a-1]++; reuse.insert(a-1);
        }
        for(int i:reuse){
            if((neighbour[i].size()==1 && number[i]>=2 && number[i]%2==0) || (neighbour[i].size()>1 && number[i]%2==1)){
                vec.push_back(i); number[i] = 1;
            } else {
                number[i] = 0;
            }
        }
        if((level+vec.size())%2==1){
            cout << -1 << '\n';
            for(int i:vec) number[i] = 0;
        } else {
            if(vec.size()!=0){
                dif = 1; sort(vec.begin(),vec.end(),smaller);
                last = -1;
                for(int i:vec){
                    who.insert(i);
                    if(last!=-1) who.insert(lca(last,i));
                    last = i;
                }
                cur = 0;
                for(int i:who) fia[cur++] = i;
                dif = 0; sort(fia,fia+cur,smaller);
                f[fia[0]] = -1; a = fia[0];
                for(int i=1; i<cur; i++){
                    while(order[a][1]<order[fia[i]][0]) up();
                    f[fia[i]] = a; a = fia[i];
                }
                while(a!=-1) up();
                for(int i=0; i<cur; i++) number[fia[i]] = 0;
            }
            cout << ans-1 << '\n';
        }
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9728 KB Output is correct
2 Correct 67 ms 11904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 10240 KB Output is correct
2 Correct 24 ms 10240 KB Output is correct
3 Correct 71 ms 19440 KB Output is correct
4 Correct 105 ms 19824 KB Output is correct
5 Correct 133 ms 23076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 11008 KB Output is correct
2 Correct 35 ms 10880 KB Output is correct
3 Correct 96 ms 29560 KB Output is correct
4 Correct 152 ms 32372 KB Output is correct
5 Correct 87 ms 27384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 12820 KB Output is correct
2 Correct 37 ms 11904 KB Output is correct
3 Correct 31 ms 11512 KB Output is correct
4 Correct 23 ms 12544 KB Output is correct
5 Correct 25 ms 12800 KB Output is correct
6 Correct 58 ms 12920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 15736 KB Output is correct
2 Correct 137 ms 16084 KB Output is correct
3 Correct 108 ms 13056 KB Output is correct
4 Correct 134 ms 16080 KB Output is correct
5 Correct 131 ms 16096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 209 ms 19448 KB Output is correct
2 Correct 198 ms 24944 KB Output is correct
3 Correct 188 ms 25316 KB Output is correct
4 Correct 160 ms 24444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9728 KB Output is correct
2 Correct 67 ms 11904 KB Output is correct
3 Correct 25 ms 10240 KB Output is correct
4 Correct 24 ms 10240 KB Output is correct
5 Correct 71 ms 19440 KB Output is correct
6 Correct 105 ms 19824 KB Output is correct
7 Correct 133 ms 23076 KB Output is correct
8 Correct 28 ms 11008 KB Output is correct
9 Correct 35 ms 10880 KB Output is correct
10 Correct 96 ms 29560 KB Output is correct
11 Correct 152 ms 32372 KB Output is correct
12 Correct 87 ms 27384 KB Output is correct
13 Correct 67 ms 12820 KB Output is correct
14 Correct 37 ms 11904 KB Output is correct
15 Correct 31 ms 11512 KB Output is correct
16 Correct 23 ms 12544 KB Output is correct
17 Correct 25 ms 12800 KB Output is correct
18 Correct 58 ms 12920 KB Output is correct
19 Correct 89 ms 15736 KB Output is correct
20 Correct 137 ms 16084 KB Output is correct
21 Correct 108 ms 13056 KB Output is correct
22 Correct 134 ms 16080 KB Output is correct
23 Correct 131 ms 16096 KB Output is correct
24 Correct 209 ms 19448 KB Output is correct
25 Correct 198 ms 24944 KB Output is correct
26 Correct 188 ms 25316 KB Output is correct
27 Correct 160 ms 24444 KB Output is correct
28 Correct 132 ms 15736 KB Output is correct
29 Correct 217 ms 25336 KB Output is correct
30 Correct 122 ms 21872 KB Output is correct
31 Correct 145 ms 32372 KB Output is correct
32 Correct 125 ms 15992 KB Output is correct
33 Correct 225 ms 22392 KB Output is correct
34 Correct 261 ms 25892 KB Output is correct
35 Correct 253 ms 25596 KB Output is correct