답안 #660665

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
660665 2022-11-22T18:07:15 Z Lobo Spring cleaning (CEOI20_cleaning) C++17
28 / 100
1000 ms 11156 KB
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
// #define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

const int maxn = 1e5+10;

int n, q, gr[maxn], p[maxn], sbl[maxn];
vector<int> g[maxn];
int cntl, ans;

void dfs(int u, int ant) {
    p[u] = ant;
    if(gr[u] == 1) {
        cntl++;
        sbl[u] = 1;
    }
    else sbl[u] = 0;
    for(auto v : g[u]) if(v != ant) {
        dfs(v,u);
        sbl[u]^= sbl[v];
    }
}

void change(int v) {
    while(v != -1) {
        if(sbl[v] == 1) ans--;
        sbl[v]^= 1;
        if(sbl[v] == 1) ans++;
        v = p[v];
    }
}

void solve() {
    cin >> n >> q;
    for(int i = 1; i <= n-1; i++) {
        int u,v; cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
        gr[u]++;
        gr[v]++;
    }

    int rt;
    for(int i = 1; i <= n; i++) {
        if(gr[i] != 1) {
            rt = i;
            break;
        }
    }
    dfs(rt,-1);

    for(int i = 1; i <= n; i++) {
        if(sbl[i] == 1) ans++;
    }

    while(q--) {
        int D; cin >> D;
        vector<int> as;
        for(int i = 0; i < D; i++) {
            int v; cin >> v;
            as.pb(v);

            if(gr[v] == 1) {
                cntl--;
                change(v);
            }
            gr[v]++;
            cntl++;
            change(v);
        }

        // for(int i = 1; i <= n; i++) {
        //     cout << i << " " << sbl[i] << endl;
        // }
        // cout << ans << endl;

        if(cntl%2 == 1) {
            cout << -1 << endl;
        }
        else {
            int q1 = ans+D;
            int q2 = n-1-ans;
            cout << q1 + 2*q2 << endl;
        }

        for(int i = 0; i < D; i++) {
            int v = as[i];
            gr[v]--;
            if(gr[v] == 1) {
                cntl++;
                change(v);
            }
            cntl--;
            change(v);
        }

    }
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    int tt = 1;
    // cin >> tt;
    while(tt--) {
        solve();
    }

}

Compilation message

cleaning.cpp: In function 'void solve()':
cleaning.cpp:59:8: warning: 'rt' may be used uninitialized in this function [-Wmaybe-uninitialized]
   59 |     dfs(rt,-1);
      |     ~~~^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2676 KB Output is correct
2 Correct 49 ms 4220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 3840 KB Output is correct
2 Correct 11 ms 3796 KB Output is correct
3 Correct 24 ms 8160 KB Output is correct
4 Correct 29 ms 7756 KB Output is correct
5 Correct 48 ms 9364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1065 ms 4344 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1077 ms 4700 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 7012 KB Output is correct
2 Correct 67 ms 6804 KB Output is correct
3 Correct 57 ms 5252 KB Output is correct
4 Correct 67 ms 6860 KB Output is correct
5 Correct 66 ms 6788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 9404 KB Output is correct
2 Execution timed out 1074 ms 11156 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2676 KB Output is correct
2 Correct 49 ms 4220 KB Output is correct
3 Correct 11 ms 3840 KB Output is correct
4 Correct 11 ms 3796 KB Output is correct
5 Correct 24 ms 8160 KB Output is correct
6 Correct 29 ms 7756 KB Output is correct
7 Correct 48 ms 9364 KB Output is correct
8 Execution timed out 1065 ms 4344 KB Time limit exceeded
9 Halted 0 ms 0 KB -