Submission #930763

# Submission time Handle Problem Language Result Execution time Memory
930763 2024-02-20T11:32:07 Z LucaIlie Spring cleaning (CEOI20_cleaning) C++17
16 / 100
1000 ms 13248 KB
#include <bits/stdc++.h>

using namespace std;

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

void dfs( int u, int 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 );
    }



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

        cin >> d;
        for ( int i = 0; i < d; i++ ) {
            cin >> a[i];
            int p = a[i], v = n + 1 + i;
            adj[p].push_back( v );
            adj[v].push_back( p );
        }

        int r = 1;
        while ( adj[r].size() == 1 )
            r++;
        ans = 0;
        dfs( r, 0 );
        ans -= 2 - leafs[r] % 2;
        cout << (leafs[r] % 2 == 0 ? ans : -1) << "\n";

        for ( int i = d - 1; i >= 0; i-- ) {
            int p = a[i], v = n + 1 + i;
            adj[p].pop_back();
            adj[v].pop_back();
        }
    }


    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Execution timed out 1084 ms 4180 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 21 ms 6748 KB Output is correct
2 Correct 21 ms 6720 KB Output is correct
3 Runtime error 42 ms 13248 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 7260 KB Output is correct
2 Correct 23 ms 7260 KB Output is correct
3 Runtime error 50 ms 12760 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 186 ms 4740 KB Output is correct
2 Correct 119 ms 3928 KB Output is correct
3 Correct 155 ms 3672 KB Output is correct
4 Correct 197 ms 4440 KB Output is correct
5 Correct 159 ms 4448 KB Output is correct
6 Correct 213 ms 4516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1022 ms 5972 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 52 ms 13140 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Execution timed out 1084 ms 4180 KB Time limit exceeded
3 Halted 0 ms 0 KB -