//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;
void solve(){
int n, q; cin >> n >> q;
vector<vector<int>>g(n+1);
for (int i = 1; i<n; i++){
int a, b; cin >> a >> b;
g[a].emplace_back(b);
g[b].emplace_back(a);
}
while (q--){
int d; cin >> d;
g.resize(n+d+1);
int u = n+1;
vector<int>vt;
auto fix = [&](){
for (auto x: vt) g[x].pop_back();
for (int i = n+1; i<u; i++) g[i].clear();
};
while (d--){
int x; cin >> x;
vt.emplace_back(x);
g[x].emplace_back(u);
g[u].emplace_back(x);
u++;
}
//<u
int l = 0;
for (int i = 1; i<u; i++){
if ((int)g[i].size() == 1){
l++;
}
}
if (l&1) {
cout << "-1\n";
fix();
continue;
}
int ans = 0;
function<int(int, int)>dfs = [&](int v, int pa){
int ile = ((int)g[v].size() == 1);
for (auto x: g[v]){
if (x == pa) continue;
ile += dfs(x, v);
}
if (v == pa) return 0ll;
if (ile&1) ans++;
else ans += 2;
return ile;
};
dfs(1, 1);
cout << ans << "\n";
fix();
}
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
456 KB |
Output is correct |
2 |
Execution timed out |
1059 ms |
2584 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
6744 KB |
Output is correct |
2 |
Correct |
13 ms |
6744 KB |
Output is correct |
3 |
Correct |
21 ms |
11904 KB |
Output is correct |
4 |
Correct |
34 ms |
12028 KB |
Output is correct |
5 |
Correct |
44 ms |
13856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
7632 KB |
Output is correct |
2 |
Correct |
13 ms |
7080 KB |
Output is correct |
3 |
Correct |
38 ms |
19520 KB |
Output is correct |
4 |
Correct |
62 ms |
23388 KB |
Output is correct |
5 |
Correct |
34 ms |
18120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
201 ms |
3996 KB |
Output is correct |
2 |
Correct |
108 ms |
2396 KB |
Output is correct |
3 |
Correct |
95 ms |
2140 KB |
Output is correct |
4 |
Correct |
10 ms |
2600 KB |
Output is correct |
5 |
Correct |
182 ms |
2948 KB |
Output is correct |
6 |
Correct |
215 ms |
2960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1031 ms |
7236 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1055 ms |
10064 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
456 KB |
Output is correct |
2 |
Execution timed out |
1059 ms |
2584 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |