답안 #1043626

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1043626 2024-08-04T12:22:38 Z beaconmc Spring cleaning (CEOI20_cleaning) C++14
34 / 100
1000 ms 15528 KB
#include <bits/stdc++.h>
 
typedef long long ll;
#define FOR(i,x,y) for(ll i=x; i<y; i++)
#define FORNEG(i,x,y) for(ll i=x; i>y; i--)
 
using namespace std;

vector<ll> edges[200005];
ll realans = 0;

int dp(ll a, ll p){
	ll ans = 0;
	if (edges[a].size() <= 1) ans = 1;
	for (auto&i : edges[a]){
		if (i != p) ans += dp(i, a);
	}		
	if (p!=-1) realans += 1 + (ans+1)%2;

	return 1 + (ans+1)%2;
}
int main(){
	ll n,q;
	cin >> n >> q;
	FOR(i,0,n-1){
		ll a,b;
		cin >> a >> b;
		edges[a].push_back(b);
		edges[b].push_back(a);
	}
	FOR(i,0,q){
		ll d; cin >> d;
		ll cur = n+1;
		FOR(k,0,d){
			ll temp; cin >> temp;
			edges[temp].push_back(cur++);
		}
		ll root = -1;
		ll cnt = d;
		FOR(j,1,n+1){
			if (edges[j].size() != 1){
				root = j;
			}else{
				cnt++;
			}
		}

		if (cnt%2==1) cout << -1 << "\n";
		else{
			realans = 0;
			dp(root, -1);
			cout << realans << "\n";
		}
		FOR(i,1,n+1){
			while (edges[i].size() && edges[i][edges[i].size()-1]>n){
				edges[i].pop_back();
			}
		}

	}
}





# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Execution timed out 1030 ms 6736 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 6492 KB Output is correct
2 Correct 12 ms 6404 KB Output is correct
3 Correct 28 ms 9772 KB Output is correct
4 Correct 36 ms 9860 KB Output is correct
5 Correct 49 ms 11204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 7000 KB Output is correct
2 Correct 14 ms 6744 KB Output is correct
3 Correct 38 ms 13908 KB Output is correct
4 Correct 55 ms 15528 KB Output is correct
5 Correct 37 ms 13140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 161 ms 6996 KB Output is correct
2 Correct 100 ms 6356 KB Output is correct
3 Correct 86 ms 5976 KB Output is correct
4 Correct 27 ms 6356 KB Output is correct
5 Correct 147 ms 6492 KB Output is correct
6 Correct 188 ms 6828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1054 ms 8844 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1055 ms 10068 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Execution timed out 1030 ms 6736 KB Time limit exceeded
3 Halted 0 ms 0 KB -