Submission #930766

# Submission time Handle Problem Language Result Execution time Memory
930766 2024-02-20T11:41:12 Z LucaIlie Spring cleaning (CEOI20_cleaning) C++17
0 / 100
1000 ms 14388 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5;
int leafs[MAX_N + 1], parent[MAX_N + 1], a[MAX_N + 1];
vector<int> adj[MAX_N + 1];
int ans;

void dfs( int u, int p ) {
    parent[u] = p;
    leafs[u] = (adj[u].size() == 1);
    for ( int v: adj[u] ) {
        if ( v == p )
            continue;
        dfs( v, u );
        leafs[u] += leafs[v];
    }
    ans += 2 - leafs[u] % 2;
}

int main() {
    int n, q;

    cin >> n >> q;
    for ( int i = 0; i < n - 1; i++ ) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back( v );
        adj[v].push_back( u );
    }

    int r = 1;
    while ( adj[r].size() == 1 )
        r++;
    ans = 0;
    dfs( r, 0 );
    ans -= 2 - leafs[r] % 2;

    for ( int i = 0; i < q; i++ ) {
        int d;
        int lf = 0, change = 0;

        cin >> d;
        for ( int i = 0; i < d; i++ ) {
            cin >> a[i];
            int v = a[i];
            change++;
            if ( adj[v].size() == 1 )
                continue;
            lf++;
            while ( v != r ) {
                change -= 2 - leafs[v] % 2;
                leafs[v]++;
                change += 2 - leafs[v] % 2;
                v = parent[v];
            }
        }

        cout << ((leafs[r] + lf) % 2 == 0 ? ans + change : -1) << "\n";

        for ( int i = 0; i < d; i++ ) {
            int v = a[i];
            if ( adj[v].size() == 1 )
                continue;
            while ( v != r ) {
                leafs[v]--;
                v = parent[v];
            }
        }
    }


    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Incorrect 40 ms 3636 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 15 ms 3164 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 855 ms 3668 KB Output is correct
2 Correct 856 ms 3680 KB Output is correct
3 Correct 484 ms 14388 KB Output is correct
4 Execution timed out 1030 ms 13608 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1046 ms 4184 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 147 ms 5932 KB Output is correct
2 Incorrect 68 ms 6480 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 214 ms 7764 KB Output is correct
2 Execution timed out 1063 ms 10888 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Incorrect 40 ms 3636 KB Output isn't correct
3 Halted 0 ms 0 KB -