답안 #430108

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
430108 2021-06-16T11:30:50 Z Ozy Spring cleaning (CEOI20_cleaning) C++17
0 / 100
1000 ms 10568 KB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define rep(i,a,b) for (lli i = (a); i <= (b); i++)
#define repa(i,a,b) for (lli i = (a); i >= (b); i--)
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "

#define MAX 100000

lli n,a,b,q,d,res,total,raiz;
vector<lli> hijos[MAX+2];
lli val[MAX+2],borr[MAX+2],padres[MAX+2];

lli DFS(lli pos) {

    if (hijos[pos].size() == 1) return 1;

    for (auto h : hijos[pos]) {
        if (h == padres[pos]) continue;
        padres[h] = pos;
        val[pos] += DFS(h);
    }

    res = !(val[pos] % 2) ? res + 1 : res;
    return val[pos];

}

void agrega(lli pos) {

    while (pos > 0) {
        borr[pos]++;

        if (borr[pos] == 1) break;

        if (borr[pos]%2) ++res;
        else  --res;

        pos = padres[pos];
    }

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> q;
    rep(i,1,n-1) {
        cin >> a >> b;
        hijos[a].push_back(b);
        hijos[b].push_back(a);

        if (raiz != 0) continue;
        if (hijos[a].size() > 1) raiz = a;
        if (hijos[b].size() > 1) raiz = b;
    }

    res = 0;
    a = DFS(raiz);
    res += n - 1;

    if (!(val[raiz])) --res;

    rep(Q,1,q) {

        rep(i,1,n) borr[i] = val[i];
        total = res;

        cin >> d;
        rep(i,1,d) {
            cin >> a;
            total++;
            agrega(a);
        }

        if (borr[raiz] % 2) cout << -1 << "\n";
        else cout << total << "\n";

    }
}

# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2636 KB Output is correct
2 Incorrect 74 ms 4764 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 3148 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 337 ms 3480 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1069 ms 4884 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1077 ms 8428 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1063 ms 10568 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2636 KB Output is correct
2 Incorrect 74 ms 4764 KB Output isn't correct