답안 #1096033

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1096033 2024-10-03T15:23:17 Z _8_8_ Spring cleaning (CEOI20_cleaning) C++17
34 / 100
1000 ms 17752 KB
#include <bits/stdc++.h>
    
using namespace std;
    
typedef long long ll;
const int  N = 2e5 + 12, MOD = (int)1e9 + 7;
 
int n, q, rt;
ll cur = 0;
int dp[N], s[N];
vector<int> g[N];
void dfs(int v, int pr = -1) {
    s[v] = 0;
    bool is = 1;
    for(int to:g[v]) {
        if(to == pr) continue;
        is = 0;
        dfs(to, v);
        s[v] += s[to];
        cur += (s[to] & 1 ? 1 : 2);
    }
    s[v] += (is);
}
void test() {
    cin >> n >> q;
    for(int i = 1; i <= n - 1; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
 
    for(int i = 1; i <= n; i++) {
        if((int)g[i].size() != 1) {
            rt = i;
            break;
        }
    }
    while(q--) {
        cur =0 ;
        int d;
        cin >> d;
        vector<int> a(d);
        for(int j = 0; j < d; j++) {       
            cin >> a[j];
            g[a[j]].push_back(n + j + 1);
        }
        dfs(rt);
        if(s[rt] & 1) {
            cur = -1;
        }
        cout << cur << '\n';
        for(int i = 0; i < d; ++i) {
            g[a[i]].pop_back();
        }
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); 
 
    int t = 1; 
    // cin >> t;
    
    while(t--) 
        test();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Execution timed out 1071 ms 6508 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 6744 KB Output is correct
2 Correct 10 ms 6748 KB Output is correct
3 Correct 17 ms 9840 KB Output is correct
4 Correct 24 ms 9672 KB Output is correct
5 Correct 29 ms 10868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 7260 KB Output is correct
2 Correct 13 ms 7260 KB Output is correct
3 Correct 31 ms 17500 KB Output is correct
4 Correct 49 ms 17752 KB Output is correct
5 Correct 27 ms 16216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 176 ms 7080 KB Output is correct
2 Correct 108 ms 6236 KB Output is correct
3 Correct 141 ms 5980 KB Output is correct
4 Correct 206 ms 6492 KB Output is correct
5 Correct 166 ms 6748 KB Output is correct
6 Correct 211 ms 6864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1066 ms 8388 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1031 ms 10120 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Execution timed out 1071 ms 6508 KB Time limit exceeded
3 Halted 0 ms 0 KB -