Submission #1002333

#TimeUsernameProblemLanguageResultExecution timeMemory
1002333yoav_sSpring cleaning (CEOI20_cleaning)C++17
34 / 100
1097 ms23384 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<vv> vvv;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef pair<ll,ll> p;
typedef vector<p> vp;
typedef vector<vp> vvp;
typedef vector<vvp> vvvp;
typedef pair<ll, p> tri;
typedef vector<tri> vtri;
typedef vector<vtri> vvtri;
typedef vector<vvtri> vvvtri;

#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()

const ll INF = 1e18;
const ll mod = 1e9 + 7;

ll getLeafCount(ll i, vv &graph, v &res)
{
    if (res[i] != -1) return 0;
    res[i] = 0;
    if (graph[i].size() == 1)
    {
        res[i] = 1;
        return 1;
    }
    for (ll x : graph[i]) res[i] += getLeafCount(x, graph, res);
    return res[i];
}


int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll N, Q;
    cin >> N >> Q;
    vv graph(N);
    for (ll i = 0; i < N - 1; i++)
    {
        ll a, b;
        cin >> a >> b; a--; b--;
        graph[a].pb(b);
        graph[b].pb(a);
    }
    for (ll j = 0; j < Q; j++)
    {
        ll amt;
        cin >> amt;
        vv newGraph(N + amt);
        for (ll i = 0; i < N; i++) for (ll x : graph[i]) newGraph[i].pb(x);
        for (ll j = 0; j < amt; j++)
        {
            ll par;
            cin >> par; par--;
            newGraph[N + j].pb(par);
            newGraph[par].pb(N + j);
        }
        ll root = 0;
        while (newGraph[root].size() == 1) root++;
        v leafCount(N + amt, -1);
        getLeafCount(root, newGraph, leafCount);
        if (leafCount[root] % 2 == 1)
        {
            cout << "-1\n";
            continue;
        }
        ll res = N + amt - 2;
        for (ll i= 0 ; i < N; i++) if (leafCount[i] % 2 == 0) res++;
        cout << res << "\n";
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...