답안 #976203

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
976203 2024-05-06T09:37:53 Z efedmrlr Spring cleaning (CEOI20_cleaning) C++17
34 / 100
1000 ms 16464 KB
// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,popcnt")
#include <bits/stdc++.h>

#define lli long long int
#define ld long double
#define REP(i, n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end();
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define MP make_pair

using namespace std;

void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

const int N = 2e5 + 5;
const int INF = 1e9 + 500;
const int MOD = 1e9 + 7;
int n, q;
vector<vector<int> > adj(N, vector<int>());
int ans = 0;
int dfs(int node, int par) {
    if(adj[node].size() == 1 && par) {
        ans++;
        // cout << "node: " << node << " 1" << "\n";
        return 1;
    }
    int ret = 0;
    if(par == 0 && adj[node].size() == 1) ret++;
    for(auto c : adj[node]) {
        if(c == par) continue;
        ret += dfs(c, node);
    }
    // cout << "node: " << node << " ret:" << ret << "\n";
    if(par == 0) {
        if(ret & 1) return -1;
        return ans;
    }
    if(ret & 1) {  
        ans++;
        return 1;
    }
    else {
        ans += 2;
        return 2;
    }
}

void solve() {
    cin >> n >> q;
    REP(i, n - 1) {
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    REP(z, q) {
        int d;
        cin >> d;
        ans = 0;
        // fill(dp.begin(), dp.begin() + n + d + 3, 0);
        for(int i = n + 1; i <= n + d; i++) {
            int x;
            cin >> x;
            adj[i].pb(x);
            adj[x].pb(i);
        }
        int res = dfs(1, 0);
        cout << res << "\n";
        for(int i = n + 1; i <= n + d; i++) {
            for(auto j : adj[i]) {
                adj[j].pop_back();
            }
            adj[i].clear();
        }

    }
}

signed main() {
    fastio();
    solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Execution timed out 1093 ms 6680 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 8540 KB Output is correct
2 Correct 12 ms 8540 KB Output is correct
3 Correct 18 ms 9424 KB Output is correct
4 Correct 28 ms 11464 KB Output is correct
5 Correct 34 ms 12564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 8792 KB Output is correct
2 Correct 16 ms 8924 KB Output is correct
3 Correct 43 ms 14104 KB Output is correct
4 Correct 57 ms 16464 KB Output is correct
5 Correct 25 ms 13148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 160 ms 6944 KB Output is correct
2 Correct 98 ms 6236 KB Output is correct
3 Correct 141 ms 5980 KB Output is correct
4 Correct 185 ms 6236 KB Output is correct
5 Correct 163 ms 6432 KB Output is correct
6 Correct 197 ms 6532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1025 ms 8016 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1022 ms 9296 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Execution timed out 1093 ms 6680 KB Time limit exceeded
3 Halted 0 ms 0 KB -